matplotlib.pyplot(plt)学习

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

在matplot结构层次的最高层是pyplot,在这一层,简单的方法用来添加绘制的基本要素(lines,images,text,etc.)到当前图(figure)中的坐标轴(axes)中。

学习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 abbreviationColor Name
bblue
ccyan
ggreen
kblack
mmagenta
rred
wwhite
yyellow
  • line styles

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

Marker abbreviationMarker style
.Point marker
,Pixel marker
oCircle marker
vTriangle down marker
^Triangle up marker
<Triangle left marker
>Triangle right marker
1Tripod down marker
2Tripod up marker
3Tripod left marker
4Tripod right marker
sSquare marker
pPentagon marker
*Star marker
hHexagon marker
HRotated hexagon marker
+Plus marker
xCross (x) marker
DDiamond marker
dThin 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)

NameAttributes
-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
fancyhead_length=0.4, head_width=0.4, tail_width=0.4
simplehead_length=0.5, head_width=0.5, tail_width=0.2
wedgetail_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
  • plt.gca()
    获得当前的坐标轴Axes。
  • 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()
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值