- Matplotlib 3.0 Cookbook
- Srinivasa Rao Poladi
- 263字
- 2025-04-04 16:06:37
How it works...
Here is the explanation for the code:
- fig, (ax1, ax2, ax3) = plt.subplots(1, 3) defines the layout of the figure with three axes objects to be plotted in one row.
- On the ax1 axes, a simple line chart is plotted with the x and y axis limits set. Due to the very large range of data, not all the points are clearly visible, and the relative gap between them is not easy to visualize.
- On ax2, a logarithmic scale is demonstrated without any chart in it. ax2.set() sets all the parameters for the plot. xscale="log" and yscale="log" set the x and the y axis to logarithmic scale. aspect=2 sets the ratio of the y-scale data to be twice the size of the x-scale data. As you can see, the distance between 102 to 103 on the y axis is twice that on the x axis.
- On ax3, a logarithmic scale and the same line chart that was plotted on ax1 are plotted here, to show the difference. Here, we can see all four points clearly and we can also see the relative distance among them. adjustable="datalim" adjusts/extends the x or the y limits to accommodate the aspect ratio specified. This attribute was omitted in the second plot (ax2), so it took the default argument of box, which adjusts the physical object size (instead of data scale) to accommodate the aspect ratio.
You should see the output figure as follows: