matplotlib-- legend(图例说明解释),annotate(标注),text(标注)
Posted guoruxin
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了matplotlib-- legend(图例说明解释),annotate(标注),text(标注)相关的知识,希望对你有一定的参考价值。
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 x = np.linspace(-3, 3, 50) 5 y1 = 2 * x + 1 6 y2 = x ** 2 7 8 plt.figure() 9 l1, = plt.plot(x, y1, color = "red", linewidth = 5.0, linestyle = ‘--‘, label = ‘down‘) #指定线的颜色, 宽度和类型 10 l2, = plt.plot(x, y2, label = "up") 11 12 #给figure添加legend(图例、说明、解释) 13 plt.legend(handles = [l1, l2], labels = ["y1 = 2 * x + 1", "y2 = x ** 2"], loc = "best") 14 15 plt.show()
1 import matplotlib.pyplot as plt 2 import numpy as np 3 4 x = np.linspace(-3, 3, 50) 5 y1 = 2 * x + 1 6 X0 =1 7 Y0 = 2 * X0 + 1 8 9 #figure 1 10 plt.figure() 11 plt.plot(x, y1) 12 plt.scatter(X0, Y0, s = 30, color = ‘black‘) #在数据线上画点 13 plt.plot([X0, X0], [Y0, 0], color = "black", linewidth = 3.0, linestyle = ‘--‘) #过一点做垂直于X轴的垂线 14 15 #坐标轴的移动 gca = “get current axis” 16 ax = plt.gca() 17 ax.spines["right"].set_color("none") 18 ax.spines["top"].set_color("none") 19 ax.xaxis.set_ticks_position("bottom") 20 ax.yaxis.set_ticks_position("left") 21 ax.spines["bottom"].set_position(("data", 0)) #Set the X and Y coordinates of the sprite simultaneously 22 ax.spines["left"].set_position(("data", 0)) 23 24 25 #在figure上做标注的两种方法 26 #方法一 27 #关于annotate的相关参数介绍,参考博文 https://blog.csdn.net/leaf_zizi/article/details/82886755 28 ########################## 29 plt.annotate(r"$2*x_0 + 1 = %s$"%Y0, xy = (X0, Y0), xytext = (+30, -30), 30 textcoords = "offset points", fontsize = 16, 31 arrowprops = dict(arrowstyle = "->", connectionstyle = "arc3, rad=.2")) 32 #方法二 33 #补充:关于下角标的添加 “_+下角标字母” 34 #关于text的相关参数介绍,参考博文 https://blog.csdn.net/TeFuirnever/article/details/88947248 35 ########################## 36 plt.text(-3.7, 3, r"$This\ is\ y1 = 2 * x + 1\ \mu\ \sigma_i\ \alpha_t$", 37 fontdict="size":16, "color":"red") 38 39 plt.show()
以上是关于matplotlib-- legend(图例说明解释),annotate(标注),text(标注)的主要内容,如果未能解决你的问题,请参考以下文章
matplotlib-- legend(图例说明解释),annotate(标注),text(标注)
python_matplotlib 输出(保存)矢量图方法;画图时图例说明(legend)放到图像外侧;Python_matplotlib图例放在外侧保存时显示不完整问题解决
Python可视化(matplotlib)图像自定义图例(Legend)
4.11Python数据处理篇之Matplotlib系列---图例,网格,背景的设置
python使用matplotlib可视化线图(line plot)并为可视化图像添加图例(legend)信息(add legend to matplotlib plot)