import pandas as pd
import seaborn as sns
# load data
df = sns.load_dataset('penguins', cache=False)
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.show()
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.show()
When I draw two plots with seaborn, in one cell, in jupyter, I get this view:
I want to draw the plots, side by side, like this:
plot1 plot2
How I should do this?
Not two plots on one figure, but two plots on two separate figures.
fig, ax = plt.subplots(1,2)
sns.plotType(someData, ax=ax[0]) # plot1
sns.plotType(someData, ax=ax[1]) # plot2
fig.show()
%html
causes the figures to plot on top of each otheripython
, not Jupyter, or recommended creating subplots..plt.savefig('file.jpg')
to save each figure to a file.jupyterlab v4.1.4
, ipython v8.2.0
, ipywidgets v8.1.2
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# load data
df = sns.load_dataset('penguins', cache=False)
# create and save figure
sns.scatterplot(data=df, x='bill_length_mm', y='bill_depth_mm', hue='sex')
plt.savefig('bill.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
# create and save new figure
sns.scatterplot(data=df, x='flipper_length_mm', y='body_mass_g', hue='sex')
plt.savefig('flipper.jpg')
plt.close() # prevents figure from being displayed when code cell is executed
**Bill**:  **Flipper**: 
HTML
and IPython.display
from IPython.display import display, HTML
display(HTML(f"<table><tr><td><img src='bill.jpg'></td><td><img src='flipper.jpg'></td></tr></table>"))
ipywidgets
and IPython.display
import ipywidgets as widgets
import IPython.display as display
# read the image files
img1 = open('bill.jpg', 'rb').read()
img2 = open('flipper.jpg', 'rb').read()
# create the image widgets
widget1 = widgets.Image(value=img1, format='jpeg')
widget2 = widgets.Image(value=img2, format='jpeg')
# create a box widget for the image widgets
box = widgets.Box([widget1, widget2])
# display box
display(box)
imshow
matplotlib
and display them with imshow
, but the plot axis must also be set to off.# read images
img_A = mpimg.imread('bill.jpg')
img_B = mpimg.imread('flipper.jpg')
# create the subplot axis
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(11, 8))
# plot the images
ax1.imshow(img_A)
ax2.imshow(img_B)
# turn of the axis
ax1.axis('off')
_ = ax2.axis('off')