plt.legend图注框
时间: 2023-08-25 20:15:22 浏览: 57
对于在 matplotlib 中添加图注框,可以使用 `plt.legend()` 函数。该函数用于在图中添加图例,即解释图中不同元素的标签。
下面是一个简单的例子,演示如何添加图注框:
```python
import matplotlib.pyplot as plt
# 创建示例数据
x = [1, 2, 3, 4, 5]
y1 = [1, 2, 3, 4, 5]
y2 = [5, 4, 3, 2, 1]
# 绘制两条曲线
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
# 添加图注框
plt.legend()
# 显示图形
plt.show()
```
在上述代码中,我们首先创建了两组数据 `x` 和 `y1`、`y2`,然后使用 `plt.plot()` 函数绘制了两条曲线。接着,使用 `plt.legend()` 函数添加了图注框,它会根据 `plot()` 函数中的 `label` 参数来生成标签。
最后,使用 `plt.show()` 函数显示图形,即可看到带有图注框的图像。
你可以根据自己的需求进行更改和扩展,例如设置图注框的位置、字体大小等。有关更多详细信息,请参考 matplotlib 的官方文档。
相关问题
帮我改写下面的代码,是其结果不变但是表达方式不同:# 2、人体的温度是否服从正态分布? #分别用kstest、shapiro、normaltest来验证分布系数 from scipy import stats ks_test = stats.kstest(file['Temperature'], 'norm') shapiro_test = stats.shapiro(file['Temperature']) normaltest_test = stats.normaltest(file['Temperature'],axis=0) print('ks_test:',ks_test) print('shapiro_test:',shapiro_test) print('normaltest_test:',normaltest_test) # 绘制拟合正态分布曲线 Temperature = file['Temperature'] plt.figure() Temperature.plot(kind = 'kde') #原始数据的正态分布 M_S = stats.norm.fit(Temperature) #正态分布拟合的平均值loc,标准差 scale normalDistribution = stats.norm(M_S[0], M_S[1]) # 绘制拟合的正态分布图 x = np.linspace(normalDistribution.ppf(0.01), normalDistribution.ppf(0.99), 100) plt.plot(x, normalDistribution.pdf(x), c='orange') plt.xlabel('Temperature about Person') plt.title('Temperature on NormalDistribution', size=20) plt.legend(['Temperature', 'NormDistribution'])
下面是代码改写后的版本:
```
# 2、人体的温度是否服从正态分布?
# 分别使用 kstest、shapiro 和 normaltest 进行分布检验
from scipy import stats
# 使用 kstest 进行分布检验
ks_statistic, ks_pvalue = stats.kstest(file['Temperature'], 'norm')
print('ks_statistic:', ks_statistic)
print('ks_pvalue:', ks_pvalue)
# 使用 shapiro 进行分布检验
shapiro_statistic, shapiro_pvalue = stats.shapiro(file['Temperature'])
print('shapiro_statistic:', shapiro_statistic)
print('shapiro_pvalue:', shapiro_pvalue)
# 使用 normaltest 进行分布检验
normaltest_statistic, normaltest_pvalue = stats.normaltest(file['Temperature'], axis=0)
print('normaltest_statistic:', normaltest_statistic)
print('normaltest_pvalue:', normaltest_pvalue)
# 绘制拟合正态分布曲线
temperature = file['Temperature']
plt.figure()
temperature.plot(kind='kde')
# 绘制拟合的正态分布图
mu, sigma = stats.norm.fit(temperature)
norm_distribution = stats.norm(mu, sigma)
x = np.linspace(norm_distribution.ppf(0.01), norm_distribution.ppf(0.99), 100)
plt.plot(x, norm_distribution.pdf(x), c='orange')
plt.xlabel('Temperature of Person')
plt.title('Normal Distribution of Temperature', size=20)
plt.legend(['Temperature', 'Normal Distribution'])
```
注:主要的改动包括:变量名的修改、拆分 kstest、shapiro 和 normaltest 的输出、正态分布参数的获取方式等。
matplotlib 图注
要在 matplotlib 中添加图注,可以使用 plt.legend() 函数。该函数可以在图中添加一个图注,用于标识不同的数据系列。例如:
```python
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y1 = [1, 3, 5, 7]
y2 = [2, 4, 6, 8]
plt.plot(x, y1, label='Line 1')
plt.plot(x, y2, label='Line 2')
plt.legend()
plt.show()
```
在这个例子中,我们使用 plt.plot() 函数绘制了两条线,并使用 label 参数为它们分别命名。然后,我们调用 plt.legend() 函数来添加图注。该函数会自动将所有带有 label 参数的线条添加到图注中。