注解
Click here 下载完整的示例代码
轮廓线图案填充¶
演示用阴影图案填充的轮廓图。
import matplotlib.pyplot as plt
import numpy as np
# invent some numbers, turning the x and y arrays into simple
# 2d arrays, which make combining them together easier.
x = np.linspace(-3, 5, 150).reshape(1, -1)
y = np.linspace(-3, 5, 120).reshape(-1, 1)
z = np.cos(x) + np.sin(y)
# we no longer need x and y to be 2 dimensional, so flatten them.
x, y = x.flatten(), y.flatten()
图1:最简单的带颜色条的阴影图
fig1, ax1 = plt.subplots()
cs = ax1.contourf(x, y, z, hatches=['-', '/', '\\', '//'],
cmap='gray', extend='both', alpha=0.5)
fig1.colorbar(cs)
出:
<matplotlib.colorbar.ColorbarPatch object at 0x7faa0d2d3dd8>
图2:无颜色图案填充的图例图
fig2, ax2 = plt.subplots()
n_levels = 6
ax2.contour(x, y, z, n_levels, colors='black', linestyles='-')
cs = ax2.contourf(x, y, z, n_levels, colors='none',
hatches=['.', '/', '\\', None, '\\\\', '*'],
extend='lower')
# create a legend for the contour set
artists, labels = cs.legend_elements()
ax2.legend(artists, labels, handleheight=2)
plt.show()
工具书类¶
本例中显示了以下函数、方法和类的使用:
import matplotlib
matplotlib.axes.Axes.contour
matplotlib.pyplot.contour
matplotlib.axes.Axes.contourf
matplotlib.pyplot.contourf
matplotlib.figure.Figure.colorbar
matplotlib.pyplot.colorbar
matplotlib.axes.Axes.legend
matplotlib.pyplot.legend
matplotlib.contour.ContourSet
matplotlib.contour.ContourSet.legend_elements
出:
<function ContourSet.legend_elements at 0x7faa1665f158>
脚本的总运行时间: (0分1.027秒)
关键词:matplotlib代码示例,codex,python plot,pyplot Gallery generated by Sphinx-Gallery