“Matplotlib”的版本间差异
		
		
		
		
		
		跳到导航
		跳到搜索
		
				
		
		
	
| 无编辑摘要 |  (→plot) | ||
| 第2行: | 第2行: | ||
| * 参见 [https://matplotlib.org/index.html] [http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html] | * 参见 [https://matplotlib.org/index.html] [http://www.cnblogs.com/wei-li/archive/2012/05/23/2506940.html] | ||
| ==plot== | ==简单plot== | ||
| *'''import matplotlib.plot as plt''' | *'''import matplotlib.plot as plt''' | ||
| *初始化 clear | *初始化 clear | ||
|  plt.clf() |  plt.clf() | ||
| * 显示图像 | |||
| ⚫ | |||
| * Tweak spacing to prevent clipping of ylabel | * Tweak spacing to prevent clipping of ylabel | ||
|  plt.tight_layout() |  plt.tight_layout() | ||
| *plt.plot() 中的颜色,线型等 | |||
| ⚫ | |||
|  import numpy as np | |||
|  import matplotlib.pyplot as plt | |||
| ⚫ | |||
| ⚫ | |||
|  plt.clf()  #clear the current figure | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| ⚫ | |||
| :* options for the color characters are: 'r' , 'g' , 'b' = blue, 'c' = cyan, 'm' = magenta, 'y' = yellow, 'k' = black, 'w' = white | :* options for the color characters are: 'r' , 'g' , 'b' = blue, 'c' = cyan, 'm' = magenta, 'y' = yellow, 'k' = black, 'w' = white | ||
| :* Options for line styles are:  '-' = solid, '--' = dashed, ':' = dotted, '-.' = dot-dashed, '.' = points,  'o' = filled circles,  '^' = filled triangles | :* Options for line styles are:  '-' = solid, '--' = dashed, ':' = dotted, '-.' = dot-dashed, '.' = points,  'o' = filled circles,  '^' = filled triangles | ||
| :* marker style [https://matplotlib.org/api/markers_api.html] | :* marker style [https://matplotlib.org/api/markers_api.html] | ||
| ⚫ | |||
| :semilogx  #x轴对数 | |||
| :semilogy  #y轴对数 | |||
| :set_xscale("log", nonposx='clip') | |||
| :set_yscale("log", nonposy='clip') | |||
| * 误差棒  | |||
| ⚫ | |||
| *grid | |||
| :ax.grid() | |||
| *设置坐标轴的极限 | |||
| :ax.set_ylim(ymin=0.1) | |||
| :参见[https://matplotlib.org/gallery/scales/log_demo.html] | |||
| *产生多个图形窗口 | *产生多个图形窗口 | ||
| 第46行: | 第20行: | ||
|  plt.figure(2) |  plt.figure(2) | ||
| ⚫ | |||
| *pmesh,pcolormesh: 画二维的平面分布的图 | |||
| ⚫ | |||
| *colorbar | |||
| ⚫ | |||
| ⚫ | |||
|  plt.plot(x, y,label='sin') | |||
| ⚫ | |||
| ⚫ | |||
|  plt.xscale('symlog') # 设置对数坐标格式,, | |||
|  plt.xlabel('x label') | |||
|  plt.ylabel('y label') | |||
|  plt.title("Simple Plot") | |||
|  plt.legend() #图例  显示前面plot中的label | |||
| == coding style == | |||
| *通过关键词来配置图的要素,例子 | |||
|  x = np.arange(0, 10, 0.2) | |||
|  y = np.sin(x) | |||
|  fig, ax = plt.subplots() | |||
| ⚫ | |||
|  plt.show() | |||
| *axes的配置 | |||
| ⚫ | |||
| :ax.grid :添加grid | |||
| :ax.set_xlim(xmin=1,xmax=10) #设置坐标范围 | |||
| ==直方图== | ==直方图== | ||
2019年4月22日 (一) 08:02的版本
简单plot
- import matplotlib.plot as plt
- 初始化 clear
plt.clf()
- 显示图像
plt.show()
- Tweak spacing to prevent clipping of ylabel
plt.tight_layout()
- plt.plot() 中的颜色,线型等
- options for the color characters are: 'r' , 'g' , 'b' = blue, 'c' = cyan, 'm' = magenta, 'y' = yellow, 'k' = black, 'w' = white
- Options for line styles are: '-' = solid, '--' = dashed, ':' = dotted, '-.' = dot-dashed, '.' = points, 'o' = filled circles, '^' = filled triangles
- marker style [3]
 
- 产生多个图形窗口
plt.figure(1) plt.figure(2)
- 简单的例子
x = np.arange(0, 5, 0.1);
y = np.sin(x)
plt.subplot(211) # panels
plt.plot(x, y,label='sin')
plt.errorbar(x, y, xerr=0.1 * x, yerr=5.0 + 0.75 * y, ls='None', marker='s') #误差棒,ls='None' 表示不连线 
plt.xlim(0,3)  #调整坐标范围
plt.xscale('symlog') # 设置对数坐标格式,,
plt.xlabel('x label')
plt.ylabel('y label')
plt.title("Simple Plot")
plt.legend() #图例  显示前面plot中的label
coding style
- 通过关键词来配置图的要素,例子
x = np.arange(0, 10, 0.2) y = np.sin(x) fig, ax = plt.subplots() ax.plot(x, y) plt.show()
- axes的配置
- ax.semilogx : 对数坐标
- ax.grid :添加grid
- ax.set_xlim(xmin=1,xmax=10) #设置坐标范围
直方图
- hist 命令
- 关键词有 bins=None, range=None, density=None, weights=None, cumulative=False, bottom=None, histtype='bar', align='mid', orientation='vertical', rwidth=None, log=False, color=None, label=None, stacked=False, normed=None
# the histogram of the data n, bins, patches = ax.hist(x, 50, normed=1)
图像
- matplotlib里面可以用axes.imshow()
delta = 0.025
x = y = np.arange(-3.0, 3.0, delta)
X, Y = np.meshgrid(x, y)
Z = np.exp(-X**2 - Y**2)
fig, ax = plt.subplots()
im = ax.imshow(Z, interpolation='bilinear', cmap=cm.RdYlGn,
              origin='lower', extent=[-3, 3, -3, 3],
              vmax=abs(Z).max(), vmin=-abs(Z).max())     #lower 就是把index[0,0]放在左下,extent是数轴上标志的范围
- pylot.imshow(Z)
保存图片文件
- plt.savefig("filename.png")
- plt.savefig('SFH_LMC_miles.pdf',format='pdf')
- 保存文件一片空白
- 在 plt.show() 后调用了 plt.savefig() ,在 plt.show() 后实际上已经创建了一个新的空白的图片(坐标轴),这时候你再 plt.savefig() 就会保存这个新生成的空白图片。
- plt.show() 放在最后,或者
 
    # gcf: Get Current Figure
   fig = plt.gcf()
   plt.show()
   fig1.savefig('tessstttyyy.png', dpi=100)