- Matplotlib 3.0 Cookbook
- Srinivasa Rao Poladi
- 163字
- 2025-04-04 16:06:37
How to do it...
The following code block plots three graphs, one on a linear scale and the other two on a logarithmic scale. The two logarithmic graphs also demonstrate how the physical size and the data scale can be adjusted:
- Define the figure and its layout:
fig, (ax1, ax2, ax3) = plt.subplots(1, 3)
- Plot a line graph on a linear scale on axis ax1:
ax1.plot([0.1, 5, 10, 500], [0.01, 25, 100, 10000], "x-")
ax1.set(title="Linear Scale", xlim=(1e1, 1e3), ylim=(1e2, 1e4))
- Draw an empty plot with a logarithmic scale on both x and y axes:
ax2.set(title="adjustable = box", xscale="log", yscale="log", xlim=
(1e1, 1e3), ylim=(1e2, 1e3), aspect=2)
- Plot a line graph on ax3 with a logarithmic scale on both the x and the y axis:
ax3.plot([0.1, 5, 10, 500], [0.01, 25, 100, 10000], "o-")
ax3.set(title="adjustable = datalim", xscale="log", yscale="log",
adjustable="datalim", xlim=(1e-1, 1e3),
ylim=(1e-2, 1e4), aspect=1)
- Adjust the space in between the plots and display the figure on the screen:
plt.tight_layout()
plt.show()