importmatplotlib.pyplotasplt x=[0,1,2,3,4]y1=[0,1,4,9,16]y2=[0,2,4,6,8]plt.plot(x,y1,label='Line 1')plt.plot(x,y2,label='Line 2')plt.xlabel('X')plt.ylabel('Y')plt.title('Two Lines Plot')plt.legend()plt.show() Python Copy Output: 在这个简单的例子中,我们定义了...
# 绘制第一条线 ax.plot(x, y1, label='sin(x)') ax.set_xlabel('x') ax.set_ylabel('y') ax.set_title('Multiple Lines on a Graph') ax.set_xticks([0, 2.5, 5, 7.5, 10]) # 设置刻度位置 ax.set_xticklabels(['0', '2.5', '5', '7.5', '10']) # 设置刻度标签 ax.legend(...
然后使用plot函数分别绘制了这三组数据的线条,并通过label参数指定了每条线的标签。最后使用legend函数添加了图例,title函数添加了标题,xlabel和ylabel函数分别添加了x轴和y轴的标签。最后使用show函数显示了图形。 这个例子展示了如何使用Matplotlib比较图形中的多条线。在实际应用中,可以根据具体需求调整数据和样式,以...
y3,label='sqrt(x) - how2matplotlib.com')# 使用坐标指定位置plt.legend(ncol=3,loc=(0.1,0.9))# (x, y)坐标,范围在0到1之间plt.title('Power Functions with Custom Legend Position')plt.xlabel('
(0,10,100)# 多条直线y1=0.5*x+1y2=x-2y3=-0.5*x+10# 绘制直线plt.plot(x,y1,label='y = 0.5x + 1',color='b')plt.plot(x,y2,label='y = x - 2',color='r')plt.plot(x,y3,label='y = -0.5x + 10',color='g')# 添加标题和标签plt.title('Multiple Lines')plt.xlabel('x...
()) ax2.yaxis.label.set_color(p2.get_color()) #倾斜的坐标轴ticklabel plt.setp(ax1.get_xticklabels(), rotation=30, ha="right",rotation_mode="anchor") #格网 ax1.grid(which='both',ls='--',alpha=0.4) #图例 lines=[p1,p2] ax1.legend(lines,[i.get_label() for i in lines]...
Example:: >>> plot([1,2,3], [1,2,3], 'go-', label='line 1', linewidth=2) >>> plot([1,2,3], [1,4,9], 'rs', label='line 2') If you make multiple lines with one plot command, the kwargs apply to all those lines. Here is a list of available `.Line2D` ...
multiple_lines.py import matplotlib.pyplot as plt # Data x = [1, 2, 3, 4, 5] y1 = [2, 3, 5, 7, 11] y2 = [1, 4, 6, 8, 10] # Create multiple lines plt.plot(x, y1, label="Line 1") plt.plot(x, y2, label="Line 2") ...
ylabel('y label name') plt.show() 2.2 Change marker transparency from matplotlib import pyplot as plt plt.scatter(x_values, y_values, alpha = 0.1) plt.show() 2.3 Change size of dot from matplotlib import pyplot as plt np_pop = np.array(pop) np_pop = np_pop*2 # Double np_pop ...
label.set_bbox(dict(facecolor='white', edgecolor='None', alpha=0.65 )) ... 1. 2. 3. 4. 5. 图像、子图、坐标轴和记号 到目前为止,我们都用隐式的方法来绘制图像和坐标轴。快速绘图中,这是很方便的。我们也可以显式地控制图像、子图、坐标轴。Matplotlib 中的「图像」指的是用户界面看到的整个窗口...