zoukankan      html  css  js  c++  java
  • matplotlib(3)-- 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()
  • 相关阅读:
    Python3之random模块常用方法
    Go语言学习笔记(九)之数组
    Go语言学习笔记之简单的几个排序
    Go语言学习笔记(八)
    Python3之logging模块
    Go语言学习笔记(六)
    123. Best Time to Buy and Sell Stock III(js)
    122. Best Time to Buy and Sell Stock II(js)
    121. Best Time to Buy and Sell Stock(js)
    120. Triangle(js)
  • 原文地址:https://www.cnblogs.com/guoruxin/p/11247094.html
Copyright © 2011-2022 走看看