Plot two histograms on single chart with matplotlib

Here you have a working example: import random import numpy from matplotlib import pyplot x = [random.gauss(3,1) for _ in range(400)] y = [random.gauss(4,2) for _ in range(400)] bins = numpy.linspace(-10, 10, 100) pyplot.hist(x, bins, alpha=0.5, label=”x”) pyplot.hist(y, bins, alpha=0.5, label=”y”) pyplot.legend(loc=”upper right”) pyplot.show()

How to change plot background color?

Use the set_facecolor(color) method of the axes object, which you’ve created one of the following ways: You created a figure and axis/es together fig, ax = plt.subplots(nrows=1, ncols=1) You created a figure, then axis/es later fig = plt.figure() ax = fig.add_subplot(1, 1, 1) # nrows, ncols, index You used the stateful API (if you’re doing … Read more

Matplotlib different size subplots

Another way is to use the subplots function and pass the width ratio with gridspec_kw matplotlib Tutorial: Customizing Figure Layouts Using GridSpec and Other Functions matplotlib.gridspec.GridSpec has available gridspect_kw options import numpy as np import matplotlib.pyplot as plt # generate some data x = np.arange(0, 10, 0.2) y = np.sin(x) # plot it f, (a0, … Read more

“UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.” when plotting figure with pyplot on Pycharm

Solution 1: is to install the GUI backend tk I found a solution to my problem (thanks to the help of ImportanceOfBeingErnest). All I had to do was to install tkinter through the Linux bash terminal using the following command: sudo apt-get install python3-tk instead of installing it with pip or directly in the virtual … Read more

Scatter plot with different text at each data point

I’m not aware of any plotting method which takes arrays or lists but you could use annotate() while iterating over the values in n. import matplotlib.pyplot as plt y = [2.56422, 3.77284, 3.52623, 3.51468, 3.02199] z = [0.15, 0.3, 0.45, 0.6, 0.75] n = [58, 651, 393, 203, 123] fig, ax = plt.subplots() ax.scatter(z, y) … Read more

How to add a title to each subplot

ax.title.set_text(‘My Plot Title’) seems to work too. fig = plt.figure() ax1 = fig.add_subplot(221) ax2 = fig.add_subplot(222) ax3 = fig.add_subplot(223) ax4 = fig.add_subplot(224) ax1.title.set_text(‘First Plot’) ax2.title.set_text(‘Second Plot’) ax3.title.set_text(‘Third Plot’) ax4.title.set_text(‘Fourth Plot’) plt.show()