matplotlib.pyplot画图笔记

一、简单示例

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3
 4 x = np.arange(3,8,1)    #x轴数据
 5 y1 = x*2
 6 y2 = x**2
 7 plt.figure(figsize=(5,3.5))
 8 plt.plot(x, y1,‘r‘,marker=‘o‘,label=‘y1:double x‘)  #关键句,前两个参数是X、Y轴数据,其他参数指定曲线属性,如标签label,颜色color,线宽linewidth或lw,点标记marker
 9 plt.plot(x,y2,‘g‘,marker=‘^‘,label=‘y2:square of x‘)
10 plt.legend(loc=‘best‘)  #显示图例,前提是plot参数里写上label;loc是图例的位置
11 plt.xlabel(‘x(ms)‘)
12 plt.ylabel(‘y‘)
13 plt.title(‘a simple example‘)
14 #plt.savefig(‘G:/YDPIC/example.png‘,dpi=80)  #除了png,还有一些格式如svg,dpi是dot per inch每英寸的像素点数,缺省值80,论文写作一般要求1200或者矢量图
15 plt.show()  #show函数显示图表会中断程序,直到关闭图像。不要把show写在savefig前,否则保存图像一片空白。最好是注释掉savefig或者show其中一行。

matplotlib官网对plot的说明

二、面向对象方式绘图

Figure对象,是一个“画布或者容器” 的东西。如果用户没有写plt.figure(),程序会自动创建一个Figure对象(图像窗口)。 Axes是子图,一个figure上可以有多个子图,是具体作图的区域。 Axis是坐标轴。关系如下图。

在"一、简单示例"的代码中,plt帮我们隐藏了创建对象的过程,使用起来简单,能满足大部分作图的需求。查看plt.plot的源码发现ax = gca() 意思是get current axes获取当前子图,然后作图都是调用的"ax"对象的方法。 当需要在一个figure上画多子图的时候,面向对象方式绘图很好用。
给出几个例子

 绘制多子图subplot

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3
 4 x = np.arange(0,5,0.01)
 5 y1 = np.cos( 2*np.pi*x )*np.exp(-x)
 6 y2 = np.cos( 2*np.pi*x )
 7
 8 fig = plt.figure(facecolor=‘y‘)
 9
10 ax1 = fig.add_subplot(211)  #2行1列第1幅图
11 ax1.plot(x, y1, ‘.-‘)
12 ax1.set_title(‘A table of 2 subplots‘)
13 ax1.set_ylabel(‘Damped oscillation‘)
14
15 ax2 = fig.add_subplot(212)  #2行1列第2幅图
16 ax2.plot(x,y2, ‘.-‘ )
17 ax2.set_xlabel(‘time(s)‘)
18 ax2.set_ylabel(‘Undamped‘)
19
20 plt.show()

双Y轴 ax2 = ax1.twinx()

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(0.01,np.e, 0.01)
y1 = np.exp(-x)
y2 = np.log(x)

fig = plt.figure()

ax1 = fig.add_subplot(111)
ax1.plot(x, y1,‘r‘,label=‘exp(-x)‘)
ax1.legend(bbox_to_anchor=(1,0.5))
ax1.set_ylabel(‘Y values for exp(-x)‘,color=‘r‘)
ax1.set_xlabel(‘X values‘)
ax1.set_title(‘Double Y axis‘)

ax2 = ax1.twinx()
ax2.plot(x,y2,‘g‘,label=‘log(x)‘)
ax2.legend(bbox_to_anchor=(1,0.6))
ax2.set_ylabel(‘Y values for log(x)‘,color=‘g‘)

plt.show()

移动坐标轴spines

 1 import matplotlib.pyplot as plt
 2 import numpy as np
 3
 4 fig = plt.figure(figsize=(4,4))
 5 ax = fig.add_subplot(111)
 6 theta = np.arange(0, 2*np.pi, 2*np.pi/100)
 7 x = np.cos(theta)
 8 y = np.sin(theta)
 9 ax.plot(x,y)
10 # top和right颜色置为none从而看不见
11 ax.spines[‘top‘].set_color(‘none‘)
12 ax.spines[‘right‘].set_color(‘none‘)
13 # spines的set_position函数,移动坐标轴位置
14 ax.spines[‘bottom‘].set_position((‘data‘,0))
15 ax.spines[‘left‘].set_position((‘data‘,0))
16
17 ax.xaxis.set_ticks([-1.2,1.2])
18 ax.yaxis.set_ticks([-1.2,1.2])
19 plt.show()

set_postion()函数 接收的参数是一个二元组tuple (postion type, amount) position type的取值可以有:‘outward‘, ‘axes‘,‘data‘三种。本例中选择了‘‘data‘ amount取的0 就是数据坐标为0的位置。官网的解释如下:

参考博客:
https://morvanzhou.github.io/tutorials/data-manipulation/plt/4-2-subplot2/
http://hyry.dip.jp/tech/book/page/scipy/matplotlib_fast_plot.html

时间: 2024-08-30 00:32:23

matplotlib.pyplot画图笔记的相关文章

Python中用matplotlib.pyplot画图总结

1. import numpy as np import matplotlib.pyplot as plt def f1(t):     #根据横坐标t,定义第一条曲线的纵坐标     return np.exp(-t)*np.cos(2*np.pi*t) def f2(t):     #根据横坐标t,定义第二条曲线的纵坐标     return np.sin(2*np.pi*t)*np.cos(3*np.pi*t) #定义很坐标的值,来自于np.arange(0.0,5.0,0.02), #.

python数据可视化——matplotlib 用户手册入门:pyplot 画图

参考matplotlib官方指南: https://matplotlib.org/tutorials/introductory/pyplot.html#sphx-glr-tutorials-introductory-pyplot-py pyplot是常用的画图模块,功能非常强大,下面就来见识下它的能力吧 1.快速画出常见图形 2.使用关键字字符串作图 3.使用类别变量画图 4.创建多图 1 import matplotlib.pyplot as plt 2 %matplotlib inline

Python画图笔记(matplotlib)

matplotlib的官方网址:http://matplotlib.org/ matplotlib可以嵌入tex代码,画出的图形添加文字更加的漂亮. import matplotlib.pyplot as plt import numpy as np x = np.arange(-4, 4, 0.1) f1 = np.power(10, x) f2 = np.power(np.e, x) f3 = np.power(2, x) plt.plot(x, f1, 'r', x, f2, 'b', x

【python】matplotlib.pyplot入门

matplotlib.pyplot介绍 matplotlib的pyplot子库提供了和matlab类似的绘图API,方便用户快速绘制2D图表. matplotlib.pyplot是命令行式函数的集合,每一个函数都对图像作了修改,比如创建图形,在图像上创建画图区域,在画图区域上画线,在线上标注等. 下面简单介绍一下pyplot的基本使用: (1)使用plot()函数画图 plot()为画线函数,下面的小例子给ploy()一个列表数据[1,2,3,4],matplotlib假设它是y轴的数值序列,然

ubuntu 16.04 + python + matplotlib下画图显示中文设置

一.需求 因为在python画图显示的时候,经常需要展示一些中文,但是ubuntu系统下按照默认安装方式安装的时候,一般是不能显示中文的,当强行给legend.xlabel.ylabel赋予中文的时候,会显示为方块 二.参考 http://blog.csdn.net/onepiece_dn/article/details/46239581 三.配置方法 (1)  显示本机的同时可用的中文和西文字体 def dispFonts(): #显示可用的中文字体,同时支持英文的 from matplotl

python matplotlib.pyplot学习记录

matplotlib是python中很强大的绘图工具,在机器学习中经常用到 首先是导入 import matplotlib.pyplot as plt plt中有很多方法,记录下常用的方法 plt.plot()该方法用来画图,第一个参数是y值,第二个参数是x值,第三个参数是由两个值构成的字符串,第一个值是颜色,第二个值是线的类型 颜色的可选值有 ‘b’ blue ‘g’ green ‘r’ red ‘c’ cyan ‘m’ magenta ‘y’ yellow ‘k’ black ‘w’ whi

Matplotlib.pyplot 常用方法

1.介绍 Matplotlib 是一个 Python 的 2D绘图库,它以各种硬拷贝格式和跨平台的交互式环境生成出版质量级别的图形.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图, 直方图,功率谱,条形图,错误图,散点图等. 2.Matplotlib基础知识 2.1.Matplotlib中的基本图表包括的元素 x轴和y轴 水平和垂直的轴线 x轴和y轴刻度 刻度标示坐标轴的分隔,包括最小刻度和最大刻度 x轴和y轴刻度标签 表示特定坐标轴的值 绘图区域 实际绘图的区域 2.2.

Python matplotlib绘图学习笔记

测试环境: Jupyter QtConsole 4.2.1Python 3.6.1 1.  基本画线: 以下得出红蓝绿三色的点 import numpy as npimport matplotlib.pyplot as plt # evenly sampled time at 200ms intervalst = np.arange(0., 5., 0.2) # red dashes, blue squares and green trianglesplt.plot(t, t, 'r--', t

matplotlib.pyplot import报错: ValueError: _getfullpathname: embedded null character in path

Environment: Windows 10, Anaconda 3.6 matplotlib 2.0 import matplotlib.pyplot 报错: ValueError: _getfullpathname: embedded null character in path 原因以及Solution: http://stackoverflow.com/questions/34004063/error-on-import-matplotlib-pyplot-on-anaconda3-f