解读:#11_4显示汉字,设置字符串刻度,设置 x 轴和 y 轴字符串标签,设置图例。 import matplotlib . pyplot as plt #用运行时配置参数对象 rcParams ,将字体配置为黑体 plt . rcParams [' font . sans - serif ']=[' Simllei '] x = range (2,26,2) y =[15,13,14,17,20,25,26,26,27,22,18,15] plt . figure ( figsize =(13,8), dpi =80) plot 函数设置的标签 label ,用于图例 legend 显示 plt . plot ( x , y , marker =' o ', mfc =' w ', label ='6H1Eim') #将前面的数值刻度,用后面的字符串刻度代替。设置刻度字体大小。 plt . xticks ( range (2,25,1),[ str ( i )+'点' for i in range (2,25,1)], fontsize =13) plt . yticks ( range ( min ( y ), max ( y )+2,2), #设置 x 轴和 y 轴标签、字体大小 plt . xlabel ('时间', fontsize =18) plt . ylabel ('温度', fontsize =18)#设置标题、字体大小 plt . title ('某城市一天气温情况', fontsize =24)#设置图例位置、字体大小 plt . legend ( loc =' upper left ', fontsize =13) plt . grid ( alpha =0.5) plt .show0 [ str ( i )+'度' for i in range ( min ( y ), max ( y )+2,2)], fontsize =13) 图例的位置 loc 有: best (自适应,默认)、 upper right (右 上)、 upper left (左上)、 lower left (左下)、 lower right (右下)、 center (中央)、 right (右侧)等。
时间: 2024-03-29 12:41:51 浏览: 10
这段代码是用于绘制一个某城市一天气温情况的折线图。其中,首先通过设置 rcParams 对象将字体配置为黑体,然后定义了 x 和 y 的值。接着,通过 plot 函数将 x 和 y 的值绘制成折线图,并设置了标签 label 和图例 legend。接下来,通过 xticks 和 yticks 函数将数值刻度用字符串刻度代替,并设置刻度字体大小。然后,通过 xlabel 和 ylabel 函数设置 x 轴和 y 轴的标签和字体大小,通过 title 函数设置标题和字体大小。最后,通过 legend 函数设置图例的位置和字体大小,通过 grid 函数添加网格线,并通过 show 函数显示图形。图例的位置 loc 有 best(自适应,默认)、upper right(右上)、upper left(左上)、lower left(左下)、lower right(右下)、center(中央)、right(右侧)等选项。
相关问题
解读:#11_5用折线图绘制正弦和余弦曲线,以及其它显示汉字方法。 import numpy as np import matplotlib . pyplot as plt import matplotlib . fontmanagerasm #使用关键参数 fname 导入字体文件(华文楷体) myfont = fm . FontProperties ( fname = r ' C :\ Windows \ Fonts \ STKAITI . ttf ') x = np . arange (0.0,2.0* np . pi +0.01,0.01) yl = np . sin ( x ) y2= np . cos ( x ) plt . plot ( x , yl , label ='正弦') plt . plot ( x ,y2, label ='余弦') #在0-360度范围,每隔30度设置1个刻度;设置相应的字符串刻度 xt = np . arange (0.0,2.0* np . pi + np . pi /12, np . pi /6) xts =['(}度'. format ( i ) for i in range (0,390,30)] # fontproperties 表示所用字体, rotation 表示刻度标识旋转角度(45度) plt . xticks ( xt , xts , fontproperties - myfont , rotation =45) plt . xlabel (' x ﹣变量', fontproperties = myfont , fontsize =18) plt . ylabel (' y ﹣正弦余弦函数值', fontproperties =' simsun ', fontsize =18)#宋体 plt . title (' sin - cos 函数图像', fontproperties =' STLITI ', fontsize =24)#隶书 plt . legend ( prop - myfont ) #图例选择字体用 prop 关键字 plt . gridO plt . tight _ layout ) plt . show (0 #自变量取值要密,才能成曲线#计算正弦函数值 #紧凑布局,能完整显示图形
这段代码是用于绘制正弦和余弦函数的折线图。首先,通过导入 numpy 和 pyplot 模块,并使用 fontmanager 模块中的 FontProperties 类导入字体文件(华文楷体),定义了 x 的取值范围,并计算出 yl 和 y2 的值。接着,通过 plot 函数将 x 和 yl、y2 绘制成折线图,并设置了标签 label 和图例 legend。然后,通过 arange 函数设置 x 轴的刻度范围和相应的字符串刻度,并使用 format 函数生成刻度字符串。接下来,使用 xticks 函数将刻度值和字符串刻度代替数值刻度,并设置刻度字体和旋转角度。通过 xlabel 和 ylabel 函数设置 x 轴和 y 轴的标签和字体大小,通过 title 函数设置标题和字体大小。最后,通过 legend 函数选择字体,并通过 grid 函数添加网格线。tight_layout 函数可以使图形布局更加紧凑,使得图形能够完整显示。图例选择字体用 prop 关键字。
# 设置横坐标主刻度为月份,并将刻度标签的格式设置为"年-月" ax.xaxis.set_major_formatter(DateFormatter('%Y-%m'))如何加图例
要在 Matplotlib 中添加图例,可以使用 `legend` 函数。以下是一个简单的示例:
```
import matplotlib.pyplot as plt
# 生成示例数据
x = [1, 2, 3, 4, 5]
y = [1, 4, 2, 3, 5]
# 绘制折线图
plt.plot(x, y, label='Data')
# 添加图例
plt.legend()
# 显示图形
plt.show()
```
在上面的代码中,我们使用 `plot` 函数绘制了一条折线,并使用 `label` 参数给它命名。然后,我们调用 `legend` 函数来添加图例。如果我们不传递任何参数给 `legend` 函数,它会自动使用 `plot` 函数中的标签来创建图例。
如果您想将图例放置在特定位置,可以在 `legend` 函数中传递 `loc` 参数。例如,以下代码将图例放置在图形的右上角:
```
plt.legend(loc='upper right')
```
您可以使用其他字符串值来设置图例的位置,例如 `'upper left'`、`'lower right'`、`'center'` 等等。