您的位置:首页 > 编程语言 > PHP开发

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

Starting
from 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

Starting
from 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

Starting
from 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

Starting
from 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()

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Matplotlib python numpy