axis_equal_demo
等轴比演示如何设置和调整具有等轴比的图像。 123456789101112131415161718192021222324252627import matplotlib.pyplot as pltimport numpy as np# Plot circle of radius 3.an = np.linspace(0, 2 * np.pi, 100)fig, axs = plt.subplots(2, 2)axs[0, 0].plot(3 * np.cos(an), 3 * np.sin(an))axs[0, 0].set_title('not equal, looks like ellipse', fontsize=10)axs[0, 1].plot(3 * np.cos(an), 3 * np.sin(an))axs[0, 1].axis('equal')axs[0, 1].set_title('equal, looks like circle', fontsize=10)axs[1, 0].plot(3 * n...
axhspan_demo
axhspan 演示创建在水平方向或垂直方向跨越轴的直线或矩形。 1234567891011121314151617181920212223242526272829import numpy as npimport matplotlib.pyplot as pltt = np.arange(-1, 2, .01)s = np.sin(2 * np.pi * t)plt.plot(t, s)# Draw a thick red hline at y=0 that spans the xrangeplt.axhline(linewidth=8, color='#d62728')# Draw a default hline at y=1 that spans the xrangeplt.axhline(y=1)# Draw a default vline at x=1 that spans the yrangeplt.axvline(x=1)# Draw a thick blue vline at x=0 that spans the upper quadrant ...
broken_axis
断轴折断轴的示例,其中y轴将切割出一部分。 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758import matplotlib.pyplot as pltimport numpy as np# 30 points between [0, 0.2) originally made using np.random.rand(30)*.2pts = np.array([ 0.015, 0.166, 0.133, 0.159, 0.041, 0.024, 0.195, 0.039, 0.161, 0.018, 0.143, 0.056, 0.125, 0.096, 0.094, 0.051, 0.043, 0.021, 0.138, 0.075, 0.109, 0.195, 0.050, 0.074, 0.079, 0.155, 0.020, 0.010, 0.061, 0.008])# Now let&...
demo_constrained_layout
使用约束布局调整轴的大小约束布局尝试调整图中子图的大小,以使轴对象和轴上的标签之间不会重叠。 有关详细信息,请参阅 “约束布局指南”;有关替代方法,请参阅 “严格布局” 指南。 12345678910import matplotlib.pyplot as pltimport itertoolsimport warningsdef example_plot(ax): ax.plot([1, 2]) ax.set_xlabel('x-label', fontsize=12) ax.set_ylabel('y-label', fontsize=12) ax.set_title('Title', fontsize=14) 如果我们不使用constrained_layout,则标签会重叠轴 1234fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=False)for ax in axs.flatten(): example_pl...
colorbar_placement
放置色块色标表示图像数据的定量范围。放置在一个图中并不重要,因为需要为它们腾出空间。 最简单的情况是将颜色条附加到每个轴: 123456789101112import matplotlib.pyplot as pltimport numpy as npfig, axs = plt.subplots(2, 2)cm = ['RdBu_r', 'viridis']for col in range(2): for row in range(2): ax = axs[row, col] pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1), cmap=cm[col]) fig.colorbar(pcm, ax=ax)plt.show() 第一列在两行中都具有相同类型的数据,因此可能需要通过调用 Figure.colorbar 和轴列表而不是单个轴来组合我们所做的颜色栏。 12345...
custom_figure_class
自定义图类您可以传递一个自定义的图构造函数,以确定是否要从默认图中派生。 这个简单的例子创建了一个带有图标题的图形。 123456789101112131415161718import matplotlib.pyplot as pltfrom matplotlib.figure import Figureclass MyFigure(Figure): def __init__(self, *args, figtitle='hi mom', **kwargs): """ custom kwarg figtitle is a figure title """ super().__init__(*args, **kwargs) self.text(0.5, 0.95, figtitle, ha='center')fig = plt.figure(FigureClass=MyFigure, figtitle=...
demo_tight_layout
使用紧凑布局调整轴的大小tight_layout 尝试调整图中子图的大小,以便轴对象和轴上的标签之间不会重叠。 有关详细信息,请参阅 “约束布局指南”;有关替代方法,请参阅 “严格布局” 指南。 12345678910111213import matplotlib.pyplot as pltimport itertoolsimport warningsfontsizes = itertools.cycle([8, 16, 24, 32])def example_plot(ax): ax.plot([1, 2]) ax.set_xlabel('x-label', fontsize=next(fontsizes)) ax.set_ylabel('y-label', fontsize=next(fontsizes)) ax.set_title('Title', fontsize=next(fontsizes)) 123fig, ax = plt.subplots()example_plot(ax)pl...
fahrenheit_celsius_scales
同一轴上的不同比例演示如何在左右y轴上显示两个刻度。 此示例使用华氏度和摄氏度量表。 1234567891011121314151617181920212223242526272829303132import matplotlib.pyplot as pltimport numpy as npdef fahrenheit2celsius(temp): """ Returns temperature in Celsius. """ return (5. / 9.) * (temp - 32)def convert_ax_c_to_celsius(ax_f): """ Update second axis according with first axis. """ y1, y2 = ax_f.get_ylim() ax_c.set_ylim(fahrenheit2celsius(y1), fahrenheit...
figure_title
为图像设置标题创建一个具有单独的子图标题和居中的图标题的图形。 123456789101112131415161718192021222324252627import matplotlib.pyplot as pltimport numpy as npdef f(t): s1 = np.cos(2*np.pi*t) e1 = np.exp(-t) return s1 * e1t1 = np.arange(0.0, 5.0, 0.1)t2 = np.arange(0.0, 5.0, 0.02)t3 = np.arange(0.0, 2.0, 0.01)fig, axs = plt.subplots(2, 1, constrained_layout=True)axs[0].plot(t1, f(t1), 'o', t2, f(t2), '-')axs[0].set_title('subplot 1')axs[0].set_xlabel('distance (m)')axs[0].set...
geo_demo
地理预测这显示了使用子图的4个可能的投影。Matplotlib还支持 Basemaps Toolkit 和 Cartopy 用于地理预测。 1import matplotlib.pyplot as plt 1234plt.figure()plt.subplot(111, projection="aitoff")plt.title("Aitoff")plt.grid(True) 1234plt.figure()plt.subplot(111, projection="hammer")plt.title("Hammer")plt.grid(True) 1234plt.figure()plt.subplot(111, projection="lambert")plt.title("Lambert")plt.grid(True) 123456plt.figure()plt.subplot(111, projection="mollweide&qu...














