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 … Read more

Secondary axis with twinx(): how to add to legend?

You can easily add a second legend by adding the line: ax2.legend(loc=0) You’ll get this: But if you want all labels on one legend then you should do something like this: import numpy as np import matplotlib.pyplot as plt from matplotlib import rc rc(‘mathtext’, default=”regular”) time = np.arange(10) temp = np.random.random(10)*30 Swdown = np.random.random(10)*100-10 Rn … Read more

How to draw vertical lines on a given plot

The standard way to add vertical lines that will cover your entire plot window without you having to specify their actual height is plt.axvline import matplotlib.pyplot as plt plt.axvline(x=0.22058956) plt.axvline(x=0.33088437) plt.axvline(x=2.20589566) OR xcoords = [0.22058956, 0.33088437, 2.20589566] for xc in xcoords: plt.axvline(x=xc) You can use many of the keywords available for other plot commands (e.g. … Read more

Adding a legend to PyPlot in Matplotlib in the simplest manner possible

Add a label= to each of your plot() calls, and then call legend(loc=”upper left”). Consider this sample (tested with Python 3.8.0): import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 20, 1000) y1 = np.sin(x) y2 = np.cos(x) plt.plot(x, y1, “-b”, label=”sine”) plt.plot(x, y2, “-r”, label=”cosine”) plt.legend(loc=”upper left”) plt.ylim(-1.5, 2.0) plt.show() Slightly modified … Read more

Remove xticks in a matplotlib plot?

The plt.tick_params method is very useful for stuff like this. This code turns off major and minor ticks and removes the labels from the x-axis. Note that there is also ax.tick_params for matplotlib.axes.Axes objects. from matplotlib import pyplot as plt plt.plot(range(10)) plt.tick_params( axis=”x”, # changes apply to the x-axis which=”both”, # both major and minor … Read more

Matplotlib make tick labels font size smaller

There is a simpler way actually. I just found: import matplotlib.pyplot as plt # We prepare the plot fig, ax = plt.subplots() # We change the fontsize of minor ticks label ax.tick_params(axis=”both”, which=”major”, labelsize=10) ax.tick_params(axis=”both”, which=”minor”, labelsize=8) This only answers to the size of label part of your question though.

How to remove axis, legends, and white padding

The axis(‘off’) method resolves one of the problems more succinctly than separately changing each axis and border. 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 adjust padding with cutoff or overlapping labels

Use: import matplotlib.pyplot as plt plt.gcf().subplots_adjust(bottom=0.15) # alternate option without .gcf plt.subplots_adjust(bottom=0.15) to make room for the label, where plt.gcf() means get the current figure. plt.gca(), which gets the current Axes, can also be used. Edit: Since I gave the answer, matplotlib has added the plt.tight_layout() function. See matplotlib Tutorials: Tight Layout Guide So I … Read more

How to change legend size with matplotlib.pyplot

You can set an individual font size for the legend by adjusting the prop keyword. plot.legend(loc=2, prop={‘size’: 6}) This takes a dictionary of keywords corresponding to matplotlib.font_manager.FontProperties properties. See the documentation for legend: Keyword arguments: prop: [ None | FontProperties | dict ] A matplotlib.font_manager.FontProperties instance. If prop is a dictionary, a new instance will … Read more