# approach 1
z = np.arange(100).reshape((10,10))
x = np.logspace(0, 4, 10)
y = np.logspace(0, 4, 10)
ax1 = subplot(121)
ax1.contour(np.log10(x), np.log10(y), z)
# approach 2
ax2 = subplot(122)
ax2.set_xscale("log")
ax2.set_yscale("log")
ax2.contour(x, y, z)
from matplotlib.colors import LogNorm
plt.pcolor(X, Y, Z1, norm=LogNorm(vmin=Z1.min(), vmax=Z1.max()), cmap='PuBu_r')
Use vmin
and vmax
keyword.
ig, axes = plt.subplots(nrows=2, ncols=2)
for ax in axes.flat:
im = ax.imshow(np.random.random((10,10)), vmin=0, vmax=1)
fig.subplots_adjust(right=0.8)
cbar_ax = fig.add_axes([0.85, 0.15, 0.05, 0.7])
fig.colorbar(im, cax=cbar_ax)
plt.axes().set_aspect('equal') # ratio of long to short dimensions
when you want the X and Y axes have the same size dimensions for each unit.
using gridspec
import matplotlib.gridspec as gridspec
# specify the overall figure size
plt.figure(figsize=(6, 6))
# specify a 2 x 2 grid
gs = gridspec.GridSpec(2, 2, width_ratios=[1, 1],
height_ratios=[1, 1])
gs.update(hspace=0.2, wspace=0.5)
# add a lower triangle and have each axis named accordingly
ax00 = plt.subplot(gs[0, 0])
ax10 = plt.subplot(gs[1, 0])
ax11 = plt.subplot(gs[1, 1])
# use each axis to make whatever plots!
- with more fine grained control
fig = plt.figure()
ax1 = fig.add_subplot(<row_no>, <col_no>, <plot_no_1>)
ax1.plot()
ax2 = fig.add_subplot(<row_no>, <col_no>, <plot_no_2>)
ax2.plot()
...
# saving all the subplots
fig.savefig("plot_name.png", bbox_inches='tight')
append _r
to the name of the colormap, e.g. use cubehelix_r
instead of cubehelix
plt.xticks(rotation=45)
Or ref
locs, labels = plt.xticks()
plt.setp(labels, rotation=90)
plt.plot(x, delay)
cbar = fig.colorbar(surf, shrink=0.5, aspect=20, fraction=.12,pad=.02)
cbar.set_label('Activation',size=18)
# access to cbar tick labels:
cbar.ax.tick_params(labelsize=5)
remove upper / right axis line
ax.tick_params(labeltop='off', labelright='off')
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticks_position('bottom')
mpl.rcParams['xtick.direction']= out
mpl.rcParmas['ytick.direction'] = out
Fix in ~/matplotlib/matplotlib.rc
# colorblind friendly color cycler
axes.prop_cycle : cycler('color', 'E69F00, 56B4E9, 009E73, 0072B2, D55E00, CC79A7, F0E442, 000000')
Alternative colormap tableau colors
control axis limits for Pandas kde plots
from pandas.plotting import scatter_matrix
axes = scatter_matrix(pointOfSalesDf[:], alpha=0.1,
figsize=(10, 10), diagonal='kde')
_ = axes[0][0].set_xlim(*sales_xlim)
_ = axes[0][0].set_ylim(*inv_xlim)
_ = axes[1][0].set_xlim(*sales_xlim)
_ = axes[1][0].set_ylim(*inv_xlim)
_ = axes[0][1].set_xlim(*inv_xlim)
_ = axes[0][1].set_ylim(*sales_xlim)
_ = axes[1][1].set_xlim(*inv_xlim)
_ = axes[1][1].set_ylim(*sales_xlim)
ax = pointOfSalesDf.OHInvUnts_WTD.plot.kde()
_ = ax.set_xlim(-1e4, 5e3)
plt.plot(x, y, title='STUFF')
plt.legend()
ref
use bbox_to_anchor
keyword
plt.legend(loc="upper left", bbox_to_anchor=(1,1))