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

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