Bookmarks
- Python graph gallery
- Seaborn
- Matplotlib
- Examples - has a lot of examples
- Userguide
- Matplotlib Colors
To try out
- pygwalker
- Bokeh
- ggplot
Aesthetics
Colors
- Colormaps - matplotlib
- Color palettes - seaborn
- Cubehelix palette to generate custom colormap
- Colorbrewer colormaps - available through seaborn
As a list of colors:
colors = sns.color_palette("Oranges", n_colors=9)
As colormap:
cmap = sns.color_palette('Oranges', as_cmap=True)
Perceptually uniform palettes:
- matplotlib: ‘viridis’, ‘plasma’, ‘inferno’, ‘magma’, ‘cividis’
- seaborn: “rocket”, “mako”, “flare”, “crest”
Stylesheet
Seaborn styles guide. Example:
sns.axes_style() #resets style
sns.set_theme(style="white")
Axes
-
Despline:
sns.despine(ax=ax, offset=True, trim=True, bottom=True)
-
I prefer removing the axes and spine. despine from seaborn generally works well, but in some cases spines still remain so I switch of visibility for those
sns.despine(ax=ax, trim=True, offset=5) ax.spines[['right', 'top']].set_visible(False)
-
more axes modifications
ax.spines['bottom'].set_color('red') ax.spines['top'].set_color('red') ax.xaxis.label.set_color('red') ax.tick_params(axis='x', colors='red') ax.tick_params(bottom=False, labelbottom=False)
Matplotlib
Figure layout
-
use subfigures + subfigures to make publication level figures in one shot
fig = plt.figure(layout='constrained', figsize=(10, 4)) subfigs = fig.subfigures(nrows=1, ncols=2, wspace=0.07, hspace=None, width_ratios=[1.5,1], height_ratios=None) axsL = subfigs[0].subplots(nrows=1, ncols=2, sharex=False, sharey=True) axsR = subfigs[1].subplots(nrows=1, ncols=2, sharex=True, sharey=False)
-
subplot_mosaic for complicated axes - guide
axs = subfigs[0].subplot_mosaic( """ abc """, width_ratios=[1,0.75,1.5], sharey=True )
-
changing gaps between subplots
plt.subplots_adjust(-0.15)
Figure axes
-
axis labels
ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis')
-
axis limits
ax.axis([350, 700, 0, 1]) ax.set_xlim() ax.xaxis.set_ticks()
-
equal axis
plt.axis('equal') ax.set_aspect('equal', adjustable='box')
-
moving axis labels, ticks to the top
ax.xaxis.tick_top() ax.xaxis.set_label_position("top")
-
rotating axis ticks
ax.set_xticklabels(ax.get_xticklabels(), rotation=90, ha="center")
Multiple x, y axis
-
guide - here
-
Make multiple y axis by:
ax_r = ax.twinx() # use ax_r as normal
-
Make multiple x axis by using
ax.twiny()
Plot
the workhorse - doc sheet
ax.plot(x,y,color='tab:orange')
ax.plot(x,y, dashes=[2, 2], label='__nolabel__', color='k') #dashed line
Scatter
ax.scatter(x, y, s=80, facecolors='none', edgecolors='r')
Fill between
- Easy to make filled plots, highlight stimulus areas, etc
- demo
ax.fill_between(x, 0, y, label='label', facecolor=('color', alpha)) # has no edge line
ax.fill_between(x, 0, y, label='label', color=('#000', 0.3)) # with a line edge
Images
Plotting images using matplotlib
Example code with an overlay:
from PIL import Image
import matplotlib.pyplot as plt
#open image
img = Image.open('image.jpg')
# resize if required
resized = img.resize((750,750))
# convert to np array for plotting
img = np.asanyarray(img)
resized = np.asanyarray(resized)
plt.imshow(img)
plt.plot(x, y, marker='o', markeredgecolor='tab:orange', markersize=10, markerfacecolor='none', zorder=1)
plt.xticks([])
plt.yticks([])
Dual axis plot
For two y-axis:
fig, ax1 = plt.subplots()
ax2 = ax1.twinx()
# ax1 for left y axis
# ax2 for right y axis
Ordering artists
-
Note: This does not work with twin axes
-
Probably works with other types of axes, not sure
-
Works with different plots. Higher zorder implies it will be on top1
-
plot(..., zorder=10)
Labels
-
Changing location orientation of x and y labels
ax.set_ylabel('Label', rotation=270) ax.yaxis.set_label_coords(1.12,0.5)
Colorbars
-
Adding an colorbar to an axes:
cbar = fig.colorbar(mpl.cm.ScalarMappable(norm= mpl.colors.Normalize(0, 1), cmap='viridis'), ax=ax, orientation='vertical', ticks=[0, 1], format=mpl.ticker.FixedFormatter(['min', 'max']))
-
Modifying cbar labels:
cbar.set_label("Label", rotation=270)
Legends
- a very nice guide for customizing legends (doc sheet)
- use
_nolegend_
to skip plots from appearing in legend2 - you can hook it to subplots or subfigures or figures.
bbox_to_anchor
coupled withloc
is a good way of moving the legend to where you would like it in the plot.
example:
legend(legend_str, loc='outside upper right', frameon=False, bbox_to_anchor=(1.0, 0.96))
Text
-
To add annotations to the text - guide (doc sheet)
ax.text(x,y,'$\mu=$%d'%(text), color=text_color, weight='bold', ha=align) # specific location on the axis ax.text(x, ax.get_ylim()[1]*0.9, 'Text',color=color, weight='bold', ha='right', va='top',rotation='vertical')
Saving figures
- Typically, I save as a pdf as svg rarely works well for me with Affinity designer.
- Using
bbox_inches='tight'
allows legends and other modifications that go beyond 1 in a normalized fig size.3
fig.savefig("figure.pdf", bbox_inches='tight')
fig.savefig("figure.png", dpi=300)
Saving multipage pdfs
from matplotlib.backends.backend_pdf import PdfPages
with PdfPages('multipage_pdf.pdf') as pdf:
# perform standard plotting
pdf.savefig(fig)
More on multipage pdf and setting pdf info (author etc) here.
Seaborn
Heatmap
Setting one color bar across multiple axes
sns.heatmap(data, ax=ax1, cmap=cmap, vmin=min, vmax=max, cbar=False, linewidths=0)
hmap = sns.heatmap(data, ax=ax2, cmap=cmap, vmin=min, vmax=max, cbar=True, linewidths=0, cbar_kws={'ticks': [min, max], 'format':mpl.ticker.FixedFormatter(['min\n(%d)'%(np.round(min)), 'max\n(%d)'%(np.round(max))])}) hmap.collections[0].colorbar.set_label('Label', rotation=270, labelpad=-5, color=color)
# annotation can be added to heatmaps too
sns.heatmap(data, cmap='flare', ax=ax, annot=True, fmt=".2f")
FacetGrid
Facegrid (guide here) is very useful to do ridgeplots and other multi-plot grids.
The downside is that this does not work with subfigures so it is hard to have multicolumn figures with different types of plots.
I tried using facetgrid for ridge plots, but the subfigure limitation made it hard to use. I found it easier to actually plot it using matplotlib.