Why do many examples use `fig, ax = plt.subplots()` in Matplotlib/pyplot/python

plt.subplots() is a function that returns a tuple containing a figure and axes object(s). Thus when using fig, ax = plt.subplots() you unpack this tuple into the variables fig and ax. Having fig is useful if you want to change figure-level attributes or save the figure as an image file later (e.g. with fig.savefig(‘yourfilename.png’)). You certainly don’t have to use the returned figure object … Read more

Matplotlib plots: removing axis, legends and white spaces

I think that the command axis(‘off’) takes care of one of the problems more succinctly than changing each axis and the border separately. It still leaves the white space around the border however. Adding bbox_inches=’tight’ to the savefig command almost gets you there, you can see in the example below that the white space left is much smaller, but still present. … Read more

How to plot an array in python?

if you give a 2D array to the plot function of matplotlib it will assume the columns to be lines: If x and/or y is 2-dimensional, then the corresponding columns will be plotted. In your case your shape is not accepted (100, 1, 1, 8000). As so you can using numpy squeeze to solve the … Read more

Matplotlib: TypeError: can’t multiply sequence by non-int of type ‘numpy.float64’

You should make x and y numpy arrays, not lists: With this change, it produces the expected plot. If they are lists, m * x will not produce the result you expect, but an empty list. Note that m is anumpy.float64 scalar, not a standard Python float. I actually consider this a bit dubious behavior of Numpy. In normal Python, multiplying a list with an … Read more

Giving graphs a subtitle in matplotlib

I want to give my graph a title in big 18pt font, then a subtitle below it in smaller 10pt font. How can I do this in matplotlib? It appears the title() function only takes one single string with a single fontsize attribute. There has to be a way to do this, but how?