欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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()

matplotlib绘图相关
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")

matplotlib绘图相关
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="->")) # 绘制的箭头类型

matplotlib绘图相关