Bin size in Matplotlib (Histogram)

Actually, it’s quite easy: instead of the number of bins you can give a list with the bin boundaries. They can be unequally distributed, too: plt.hist(data, bins=[0, 10, 20, 30, 40, 50, 100]) If you just want them equally distributed, you can simply use range: plt.hist(data, bins=range(min(data), max(data) + binwidth, binwidth)) Added to original answer … Read more

Plot yerr/xerr as shaded region rather than error bars

Ignoring the smooth interpolation between points in your example graph (that would require doing some manual interpolation, or just have a higher resolution of your data), you can use pyplot.fill_between(): from matplotlib import pyplot as plt import numpy as np x = np.linspace(0, 30, 30) y = np.sin(x/6*np.pi) error = np.random.normal(0.1, 0.02, size=y.shape) y += … Read more

Matplotlib scatterplot; color as a function of a third variable

There’s no need to manually set the colors. Instead, specify a grayscale colormap… import numpy as np import matplotlib.pyplot as plt # Generate data… x = np.random.random(10) y = np.random.random(10) # Plot… plt.scatter(x, y, c=y, s=500) plt.gray() plt.show() Or, if you’d prefer a wider range of colormaps, you can also specify the cmap kwarg to … Read more

Generating matplotlib graphs without a running X server [duplicate]

@Neil’s answer is one (perfectly valid!) way of doing it, but you can also simply call matplotlib.use(‘Agg’) before importing matplotlib.pyplot, and then continue as normal. E.g. import matplotlib as mpl mpl.use(‘Agg’) import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(10)) fig.savefig(‘temp.png’) You don’t have to use the Agg backend, as well. The pdf, … Read more

Aligning rotated xticklabels with their respective xticks

You can set the horizontal alignment of ticklabels, see the example below. If you imagine a rectangular box around the rotated label, which side of the rectangle do you want to be aligned with the tickpoint? Given your description, you want: ha=”right” n=5 x = np.arange(n) y = np.sin(np.linspace(-3,3,n)) xlabels = [‘Ticklabel %i’ % i … Read more

Adding a y-axis label to secondary y-axis in matplotlib

The best way is to interact with the axes object directly import numpy as np import matplotlib.pyplot as plt x = np.arange(0, 10, 0.1) y1 = 0.05 * x**2 y2 = -1 *y1 fig, ax1 = plt.subplots() ax2 = ax1.twinx() ax1.plot(x, y1, ‘g-‘) ax2.plot(x, y2, ‘b-‘) ax1.set_xlabel(‘X data’) ax1.set_ylabel(‘Y1 data’, color=”g”) ax2.set_ylabel(‘Y2 data’, color=”b”) plt.show()

matplotlib does not show my plot although I call pyplot.show()

If I set my backend to template in ~/.matplotlib/matplotlibrc, then I can reproduce your symptoms: ~/.matplotlib/matplotlibrc: # backend : GtkAgg backend : template Note that the file matplotlibrc may not be in directory ~/.matplotlib/. In this case, the following code shows where it is: >>> import matplotlib >>> matplotlib.matplotlib_fname() In [1]: import matplotlib.pyplot as p … Read more

How do I tell Matplotlib to create a second (new) plot, then later plot on the old one?

If you find yourself doing things like this regularly it may be worth investigating the object-oriented interface to matplotlib. In your case: import matplotlib.pyplot as plt import numpy as np x = np.arange(5) y = np.exp(x) fig1, ax1 = plt.subplots() ax1.plot(x, y) ax1.set_title(“Axis 1 title”) ax1.set_xlabel(“X-label for axis 1”) z = np.sin(x) fig2, (ax2, ax3) … Read more