matplotlib绘图相关
程序员文章站
2024-01-15 19:13:46
...
1. 设置figsize
In [1]: import matplotlib.pyplot as plt
In [2]: figsize=(1, 2)
In [3]: plt.rcParams["figure.figsize"] = figsize
2. 绘制标签
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: x = np.linspace(-1, 1, 100)
In [4]: y = 2 * x
In [5]: plt.plot(x, y, label="y=2x")
In [6]: plt.legend() # 如果没有调用,则不会显示"y=2x"
In [7]: plt.show()
3. 绘制水平轴和竖直轴的标签
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: x = np.linspace(-1, 1, 100)
In [4]: y = 2 * x
In [5]: plt.plot(x, y)
In [6]: plt.xlabel("x")
In [7]: plt.ylabel("y")
4. 绘制注解
In [1]: import numpy as np
In [2]: import matplotlib.pyplot as plt
In [3]: x = np.linspace(-1, 1, 100)
In [4]: y = 2 * x
In [5]: fig, = plt.plot(x, y) # 返回为一个长度为0的list,因此fig后需要加","
In [6]: fig.axes.annoate("middle", # 需要显示的注解文字
xy=(0,0), # 需要注解的位置坐标
xytext=(0, 1.0), # 绘制说明文字位置的左上角坐标
arrowprops=dict(arrowstype="->")) # 绘制的箭头类型