6))plt.plot(x,y1,label='Sin(x)',color='blue')plt.plot(x,y2,label='Cos(x)',color='orange')plt.plot(x,y3,label='Damped Sin(x)',color='green')# 添加图例plt.legend(title='函数类型
接下来,我们可以使用plot函数来绘制图形。这里我们以绘制一条简单的曲线为例。代码如下所示: x=[1,2,3,4,5]y=[1,4,9,16,25]plt.plot(x,y,label='y=x^2') 1. 2. 3. 步骤四:创建并调整图例大小 在绘制图形之后,我们需要创建图例,并对图例的大小进行调整。可以使用plt.legend()函数来创建图例,并...
plot(x, lineB, label = "Line B") plt.legend() plt.show()The above plot is the same as the one created in the previous example. The only difference is that, here, we have added a legend to the plot. In the plt.plot() function, we defined a new argument label = where we ...
import matplotlib.pyplot as plt fig=plt.figure() ax=fig.add_subplot(1,1,1) l1,=ax.plot(df1["day"],df1["南京"],marker="o")#这里注意是l1,一定要以逗号结尾,下面会解释 l2,=ax.plot(df1["day"],df1["无锡"],marker="o") ax.set_xticklabels(np.arange(1,32,1)) ax.legend(handles...
plt.plot(x,x) plt.plot(x,x*2) plt.plot(x,x*3) plt.plot(x,x*4) # 直接传入legendplt.legend(['生活','颜值','工作','金钱']) plt.show() 具体实现效果: 6. 调整颜色-color 传颜色参数,使用 plot() 中的 color 属性来设置,color 支持以下几种方式。
plt.gca().add_artist(l1) import matplotlib.pyplot as plt line1, = plt.plot([1,2,3], label="Line 1", linestyle='--') line2, = plt.plot([3,2,1], label="Line 2", linewidth=4) # 为第一个线条创建图例 first_legend = plt.legend(handles=[line1], loc=1) ...
plt.plot(x,x*x) plt.show() 具体实现效果: 5. 添加图例-legend 当线条过多时,我们设置不同颜色来区分不同线条。因此,需要对不同颜色线条做下标注,我们实用 legend() 接口来实现。 importnumpyasnp importmatplotlib.pyplotasplt # 显示中文 plt.rcParams['font.sans-ser...
3 再接着,我们使用numpy创建正弦,余弦曲线的点集合,并调用plot方法绘制:4 然后,我们只需要一行代码,plt.legend(loc='位置'), 把图例加上了:5 但是,我们可能满足于这种显示方式,我想要把图例单独单个显示,拆分出来,这里重要的代码就是,获取到legend ,调用add_artist方法,不然会被覆盖掉,到...
plt.legend(loc='best',fontsize=10,frameon=True) #图例就是plt.plot代码里label定义的内容 保存图片 plt.savefig(r'F:\data\case_picture.png') #一定要在show代码前 importnumpy as npimportpandas as pdimportmatplotlib.pyplot as plt plt.rcParams['font.sans-serif']=['SimHei']#解决图例显示乱码问题...
将绘制的直线坐标传递给函数plot()。 通过函数plt.show()打开Matplotlib查看器,显示绘制的图形。 【示例】根据两点绘制一条线 代码语言:javascript 代码运行次数:0 运行 AI代码解释 # 导入matplotlib模块importmatplotlib.pyplotasplt #准备要绘制点的坐标(1,2)(4,8)# 调用绘制plot方法 ...