首页 > 编程语言 > 详细

『Python』matplotlib(二)

时间:2020-05-11 16:30:18      阅读:64      评论:0      收藏:0      [点我收藏+]

1. 绘制图表组成元素的主要函数

1.1 plot()——展现量的变化趋势

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use(‘Qt5Agg‘) 

x = np.linspace(0.05, 10, 1000)
y = np.cos(x)

plt.plot(x, y, ls="-", lw=2, label="plot figure")
plt.legend()
plt.show()
技术分享图片

1.2 scatter()——寻找变量之间的关系

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use(‘Qt5Agg‘) 

x = np.linspace(0.05, 10, 1000)
y = np.random.rand(1000)

plt.scatter(x, y, label="scatter figure")
plt.legend()
plt.show()
技术分享图片

1.3 xlim()——设置x轴的数值显示范围

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use(‘Qt5Agg‘) 

x = np.linspace(0.05, 10, 1000)
y = np.random.rand(1000)

plt.scatter(x, y, label="scatter figure")
plt.legend()
plt.xlim(0.05, 10)
plt.ylim(0, 1)
plt.show()
技术分享图片

1.4 xlabel()——设置x轴的标签文本

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use(‘Qt5Agg‘) 

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="--", lw=2, c="c", label="plot figure")
plt.legend()
plt.xlabel("x-axis")
plt.ylabel("y-axis")
plt.show()
技术分享图片

1.5 grid()——绘制刻度线的网格线

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.grid(linestyle=":", color="r")
plt.show()
技术分享图片

1.6 axhline()——绘制平行于x轴的水平参考线

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.axhline(y=0.0, c="r", ls="--", lw=2)
plt.axvline(x=4.0, c="r", ls="--", lw=2)
plt.show()
技术分享图片

1.7 axvspan()——绘制垂直于x轴的参考区域

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.axvspan(xmin=4.0, xmax=6.0, facecolor="y", alpha=0.3)
plt.axhspan(ymin=0.0, ymax=0.5, facecolor="y", alpha=0.3)
plt.show()
技术分享图片

1.8 annotate()——添加图形内容细节的指向型注释文本

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.annotate(s="maximum",
             xy=(np.pi / 2, 1.0),
             xytext=((np.pi / 2) + 1.0, 0.8),
             weight="bold",
             color="b",
             arrowprops=dict(arrowstyle="->", connectionstyle="arc3", color="b")
             )
plt.show()

xy:被注释图形内容的位置坐标

xytext:注释文本的位置坐标

weight:注释文本的字体粗细风格

color:注释文本的字体颜色

arrowprops:指示被注释内容的箭头的属性字典

技术分享图片

1.9 text()——添加图形内容细节的无指向型注释文本

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.text(x=3.10, y=0.09, s="y=sin(x)", weight="bold", color="b")
plt.show()
技术分享图片

1.10 title()——添加图形内容的标题

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.title("y=sin(x)")
plt.show()
技术分享图片

1.11 legend()——表示不同图形的文本标签图例

import numpy as np
import matplotlib.pyplot as plt
import matplotlib

matplotlib.use(‘Qt5Agg‘)

x = np.linspace(0.05, 10, 1000)
y = np.sin(x)

plt.plot(x, y, ls="-.", lw=2, c="c", label="plot figure")
plt.legend()
plt.title("y=sin(x)")
plt.show()

loc参数:

  • best
  • upper right
  • upper left
  • lower left
  • lower right
  • right
  • center left
  • center right
  • lower center
  • upper center
  • center
技术分享图片

2. 常用配置参数

2.1 线型

linestylels

  • -:实线
  • --:虚线
  • -.:点划线
  • ::点线

2.2 线宽

linewidthlw

  • 浮点数

2.3 线条颜色

colorc

  • b:blue,蓝色
  • g:green,绿色
  • r:red,红色
  • c:cyan,蓝绿
  • m:magenta,洋红
  • y:yellow,黄色
  • k:black,黑色
  • w:white,白色

也可以对关键字参数color赋十六进制的RGB字符串如 color=‘#900302‘

2.4 点标记类型

marker,只能用以下简写符号表示

  • .:point marker
  • ,:pixel marker
  • o:circle marker
  • v:triangle_down marker
  • ^:triangle_up marker
  • <:triangle_left marker
  • >:triangle_right marker
  • 1:tri_down marker
  • 2:tri_up marker
  • 3:tri_left marker
  • 4:tri_right marker
  • s:square marker
  • p:pentagon marker
  • *:star marker
  • h:hexagon1 marker
  • H:hexagon2 marker
  • +:plus marker
  • x:x marker
  • D:diamond marker
  • d:thin_diamond marker
  • |:vline marker
  • _:hline marker

官网有一张属性表,先贴在这,以后有空会再补充内容的

技术分享图片

『Python』matplotlib(二)

原文:https://www.cnblogs.com/ice-coder/p/12869759.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!