Bookmarks

To try out

Aesthetics

Colors

As a list of colors:

colors = sns.color_palette("Oranges", n_colors=9)

As colormap:

cmap = sns.color_palette('Oranges', as_cmap=True)

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)
  • subfigures guide (doc sheet)

  • 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()

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

  • Demo

    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

  • Colorbar tutorial

  • Placing colorbars

  • Colorbar tick labeling demo

  • 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 with loc 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)

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

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.

Footnotes

  1. https://stackoverflow.com/questions/17431441/matplotlib-scatter-plot-to-foreground-on-top-of-a-contour-plot

  2. https://stackoverflow.com/questions/24680981/show-only-certain-items-in-legend - note, label=None did not work for me

  3. https://stackoverflow.com/a/71663610