Matplotlib tutorial(3)
2016-01-25 12:47
615 查看
Other types of plots
Regular Plots
Starting from the code below, try to reproduce the graphic on the right taking care of filled areas:import numpy as np import matplotlib.pyplot as plt n = 256 X = np.linspace(-np.pi,np.pi,n,endpoint=True) Y = np.sin(2*X) plt.plot (X, Y+1, color='blue', alpha=1.00) plt.fill_between(X, 1, Y+1, color='blue', alpha=.25) plt.plot (X, Y-1, color='blue', alpha=1.00) plt.fill_between(X, -1, Y-1, (Y-1) > -1, color='blue', alpha=.25) plt.fill_between(X, -1, Y-1, (Y-1) < -1, color='red', alpha=.25) plt.show()
Scatter Plots
Startingfrom the code below, try to reproduce the graphic on the right taking care of marker size, color and transparency.
import numpy as np import matplotlib.pyplot as plt n = 1024 X = np.random.normal(0,1,n) Y = np.random.normal(0,1,n) T = np.arctan2(Y,X) plt.axes=([0,0,1,1]) plt.scatter(X,Y,c=T,alpha=.5) plt.show()
Bar
Plots
Startingfrom the code below, try to reproduce the graphic on the right by adding labels for red bars.
import numpy as np import matplotlib.pyplot as plt n = 12 X = np.arange(n) Y1 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) Y2 = (1-X/float(n)) * np.random.uniform(0.5,1.0,n) plt.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') plt.bar(X, -Y2, facecolor='#ff9999', edgecolor='white') for x, y in zip(X, Y1): plt.text(x+0.4, y+0.01, '%.2f' % y, ha = 'center', va = 'bottom') for x, y in zip(X, -Y2): plt.text(x+0.4, y-0.01, '%.2f' % y, ha = 'center', va = 'top') plt.xlim(-.5,n), plt.xticks([]) plt.ylim(-1.25, +1.25), plt.yticks([]) plt.show()
Contour
Plots
Startingfrom the code below, try to reproduce the graphic on the right taking care of the colormap (see Colormaps below).
import numpy as np import matplotlib.pyplot as plt def f(x,y): return (1-x/2+x**5+y**3)*np.exp(-x**2-y**2) n = 256 x = np.linspace(-3,3,n) y = np.linspace(-3,3,n) X, Y = np.meshgrid(x, y) plt.axes([0.025,0.025,0.95,0.95]) plt.contourf(X, Y, f(X,Y), 8, alpha=.75, camp=plt.cm.hot) C = plt.contour(X,Y, f(X,Y),8, colors='black',linewidth=.5) plt.clabel(C,inline=1,fontsize=10) plt.xticks([]), plt.yticks([]) plt.show()
Pie
Charts
Startingfrom the code below, try to reproduce the graphic on the right taking care of colors and slices size.
import numpy as np import matplotlib.pyplot as plt n = 20 Z = np.ones(n) Z[-1] *= 2 plt.pie(Z,explode=Z*.05,colors=['%f' % (i/float(n)) for i in range(n)]) plt.gca().set_aspect('equal') plt.xticks([]), plt.yticks([]) plt.show()
Grids
import numpy as np import matplotlib.pyplot as plt ax = plt.axes([0.025,0.025,0.95,0.95]) ax.set_xlim(0,4) ax.set_ylim(0,3) ax.xaxis.set_major_locator(plt.MultipleLocator(1.0)) ax.xaxis.set_minor_locator(plt.MultipleLocator(0.2)) ax.yaxis.set_major_locator(plt.MultipleLocator(1.0)) ax.yaxis.set_minor_locator(plt.MultipleLocator(0.2)) ax.grid(which='major', axis='x', linewidth=0.75, linestyle='-', color='0.75') ax.grid(which='minor', axis='x', linewidth=0.25, linestyle='-', color='0.75') ax.grid(which='major', axis='y', linewidth=0.75, linestyle='-', color='0.75') ax.grid(which='minor', axis='y', linewidth=0.25, linestyle='-', color='0.75') ax.set_xticklabels([]) ax.set_yticklabels([]) plt.show()
Multi
Plots
import numpy as np import matplotlib.pyplot as plt fig = plt.figure() fig.subplots_adjust(bottom=0.025, left=0.025, top = 0.975, right=0.975) plt.subplot(2,1,1) plt.xticks([]), plt.yticks([]) plt.subplot(2,3,4) plt.xticks([]), plt.yticks([]) plt.subplot(2,3,5) plt.xticks([]), plt.yticks([]) plt.subplot(2,3,6) plt.xticks([]), plt.yticks([]) # plt.savefig('../figures/multiplot_ex.png',dpi=48) plt.show()
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法