Embedding small plots inside subplots in matplotlib

I wrote a function very similar to plt.axes. You could use it for plotting yours sub-subplots. There is an example… import matplotlib.pyplot as plt import numpy as np #def add_subplot_axes(ax,rect,facecolor=”w”): # matplotlib 2.0+ def add_subplot_axes(ax,rect,axisbg=’w’): fig = plt.gcf() box = ax.get_position() width = box.width height = box.height inax_position = ax.transAxes.transform(rect[0:2]) transFigure = fig.transFigure.inverted() infig_position = … Read more

matplotlib python inline on/off

You can switch the matplotlib’s backend by %matplotlib <backend>. To switch back to your system’s default backend use %matplotlib auto or just simply %matplotlib. There are many backends available such as gtk, qt, notebook, etc. I personally highly recommend the notebook (a.k.a. nbagg) backend. It is similar to inline but interactive, allowing zooming/panning from inside … Read more

show origin axis (x,y) in matplotlib plot

Using subplots is not too complicated, the spines might be. Dumb, simple way: %matplotlib inline import numpy as np import matplotlib.pyplot as plt x = np.linspace(0.2,10,100) fig, ax = plt.subplots() ax.plot(x, 1/x) ax.plot(x, np.log(x)) ax.set_aspect(‘equal’) ax.grid(True, which=”both”) ax.axhline(y=0, color=”k”) ax.axvline(x=0, color=”k”) And I get: (you can’t see the vertical axis since the lower x-limit is … Read more

Matplotlib, Consistent font using latex

To make the tex-style/mathtext text look like the regular text, you need to set the mathtext font to Bitstream Vera Sans, import matplotlib matplotlib.rcParams[‘mathtext.fontset’] = ‘custom’ matplotlib.rcParams[‘mathtext.rm’] = ‘Bitstream Vera Sans’ matplotlib.rcParams[‘mathtext.it’] = ‘Bitstream Vera Sans:italic’ matplotlib.rcParams[‘mathtext.bf’] = ‘Bitstream Vera Sans:bold’ matplotlib.pyplot.title(r’ABC123 vs $\mathrm{ABC123}^{123}$’) If you want the regular text to look like the mathtext … Read more

How do I assign multiple labels at once in matplotlib?

You can iterate over your line objects list, so labels are individually assigned. An example with the built-in python iter function: lineObjects = plt.plot(x, y) plt.legend(iter(lineObjects), (‘foo’, ‘bar’, ‘baz’))` Edit: after updating to matplotlib 1.1.1, it looks like the plt.plot(x, y), with y as a list of lists (as provided by the author of the … Read more

set ticks with logarithmic scale

import matplotlib from matplotlib import pyplot as plt fig1, ax1 = plt.subplots() ax1.plot([10, 100, 1000], [1,2,3]) ax1.set_xscale(‘log’) ax1.set_xticks([20, 200, 500]) ax1.get_xaxis().set_major_formatter(matplotlib.ticker.ScalarFormatter()) or ax1.get_xaxis().get_major_formatter().labelOnlyBase = False plt.show()

Matplotlib: Changing the color of an axis

When using figures, you can easily change the spine color with: ax.spines[‘bottom’].set_color(‘#dddddd’) ax.spines[‘top’].set_color(‘#dddddd’) ax.spines[‘right’].set_color(‘red’) ax.spines[‘left’].set_color(‘red’) Use the following to change only the ticks: which=”both” changes both the major and minor tick colors ax.tick_params(axis=”x”, colors=”red”) ax.tick_params(axis=”y”, colors=”red”) And the following to change only the label: ax.yaxis.label.set_color(‘red’) ax.xaxis.label.set_color(‘red’) And finally the title: ax.title.set_color(‘red’)

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)