matplotlib.pyplot(plt)学习

参考一篇文章:https://blog.csdn.net/qq_28485501/article/details/82222512
前言:
在Matplotlib 结构中分位三个层次,分别为:后端层,艺术层,脚本层。而pyplot就是一个脚本层应用。它为用户提供了很多的接口来让用户与后端层和艺术层一起工作。

import matplotlib.pyplot as plt

学习plt

figure与axes对象

两者是不同的概念,axes是从属于figure的
比如 : figure,axes = plt.subplots( )

  • axes (对象): self.figure.add_subplots( )
    可以添加feature,label,title ,contour and so on。

  • figure(对象):self.figure_manager.canvas.figure()

    1. figure.colorbar ( )

各个模块的所属对象的调试信息如下:

in: plt.get_current_fig_manager()
out: Figuremanager  object 0x106e1ea48

in:plt.get_current_fig_manager().figure
out:AttributeError 没有这个属性

in :plt.plot([1,2,3,4])
out:matplotlib.lines.Line2D at 0x256154152

in:plt.get_current_fig_manager().canvas
out: FigureCanvas object 0x252165131

in:plt.get_currnt_fig_manager().canvas.figure
out:matplotlib.figure.Figure at 0x2551253

in:plt.get_currnt_fig_manager().canvas.figure.axes
out : matplotlib.axes._subplots.AxesSubplot at 0x1562613

in :plt.get_currnt_fig_manager().canvas.figure.axes[0].lines
out: matplotlib.lines.Lines2D at 0x1254514521

GUI:Some features that we can add to plots

plt本质上上来说,就是作为一个GUI的功能,显示绘画的函数,并对主体的GUI进行装饰,以下的组件都是放在axes的容器中的,由axes来管理!

  • adding a grid :plt.grit( ),设置网格线
  • handling axes :plt.axis( )设置坐标轴刻度,(x,y)的四个值。
  • adding albels :plt.xlabel( ),plt.ylabel( ),增加坐标轴标签
  • adding title :plt.title( ):增加标题
  • adding a legend :plt.legend( ):增加说明各个Line2D对象注释
  • add contour :plt.contour(matrix);为三点绘制边界线

markers and line styles(图元)

  • colors

Color abbreviation Color Name
b blue
c cyan
g green
k black
m magenta
r red
w white
y yellow
  • line styles

Style abbreviation Style
- solid line
- - dashed line
-. dash-dot line
: dotted line
  • makers styles

Marker abbreviation Marker style
. Point marker
, Pixel marker
o Circle marker
v Triangle down marker
^ Triangle up marker
< Triangle left marker
> Triangle right marker
1 Tripod down marker
2 Tripod up marker
3 Tripod left marker
4 Tripod right marker
s Square marker
p Pentagon marker
* Star marker
h Hexagon marker
H Rotated hexagon marker
+ Plus marker
x Cross (x) marker
D Diamond marker
d Thin diamond marker
| Vertical line (vlinesymbol) marker
_ Horizontal line(hline symbol)marker

Finer control with keyword arguments

在这里插入图片描述

Text inside figure, annotations,and arrows

  • Text inside figure
    plt.text(point,text)
  • Annotations
    plt.annotate(text,piont)
    在这里插入图片描述
  • Arrow
    plt.arrow(x,y,dx,dy)

Name Attributes
- None
-> head_length=0.4, head_width=0.2
-[ WidthB=1.0, lengthB=0.2, angleB=None
<- head_length=0.4, head_width=0.2
<-> head_length=0.4, head_width=0.2
fancy head_length=0.4, head_width=0.4, tail_width=0.4
simple head_length=0.5, head_width=0.5, tail_width=0.2
wedge tail_width=0.3, shrink_factor=0.5

plt绘制不同图表

plt.plot : 参见下文的plot数据的过程。
plt.hist
plt.errorbar
plt.bar
plt.pie
plt.scatter
plt.polar

在这里插入图片描述

pyplot作用

是充当一个GUI的作用
figure内置在pyplot中,所以不用其余的操作,直接plt.show()直接画出GUI图像。
plt.show()的函数作用和win.mainloop()函数是一样的,如果不调用,就不会显示图像。

面向对象

返回的都是一些有容器概念的对象,它们承载图元(primitives)

  • plt.figure()
    return Figure(640x480)
  • plt.subplots()
    返回Figure object and AxesSubplot object
  • plt.add_subplot()
    返回AxesSubplot object
  • canvas是FigureCanvas的抽象的实现。由plt.get_current_fig_manager().canvas管理
  • plt.plot()
    返回(return) Line2D object
    在pyploy库中,plot(): This function calls the plot method in the current figure’s Axes object and the figure canvas’s draw* method (as identified in the preceding setup)

pyplot.plot()data的过程分析

  • Plot the given data:
  1. Get the figure manager (or create one if it doesn’t exist).
  2. Get its figure canvas.
  3. From this, get the figure object of the canvas.
  4. From the figure object, get the current axes object (or create it if it doesn’t exist).
  5. Once the figure’s axes object is available, call its plot function.
  6. The axes plot function clears the axes and creates some lines based on the provided data.
  7. Get the active figure manager.
  8. Call the figure manager’s canvas.draw() function.

GUI+Figure:

GUI提供后端,然后matplotlib提供艺术层的图形的图元和容器
GUI工具连接Figure两者可以画出一幅图像出来,不过中间需要Canvas的帮忙,例如Tk的gui则要调用

canvas = FigureCanvasTkAgg(fig, master=root)
canvas._tkcanvas.pack(side=tk.TOP, fill=tk.BOTH, expand=1)
'''show的功能'''
root.update()
root.deiconify()
root.mainloop()

猜你喜欢

转载自blog.csdn.net/qq_28485501/article/details/84675298