Matplotlib之各种图形

Refer to 莫烦Python之Matplotlib

11. Scatter 散点图

12. Bar 柱状图

import matplotlib.pyplot as plt
import numpy as np

n = 12
X = np.arange(n)
Y1 = (1 - X/ float(n)) * np.random.uniform(0.5, 1.0, n)
# zz = (1 - X/ float(n)); print("ZZ=", zz)
# print(np.arange(n) * np.arange(n))
Y2 = (1 - X/ float(n)) * np.random.uniform(0.5, 1.0, n)

# bar plots
plt.bar(X, +Y1, facecolor='#9999ff', edgecolor="red")
plt.bar(X, -Y2, facecolor="#ff9999", edgecolor="white")

# add text
for x,y in zip(X, Y1):
	# ha horizontal alignment
	plt.text(x + 0.04, y + 0.05, '%.2f'% y, ha='center', va='bottom')
	
for x,y in zip(X, -Y2):
	# ha horizontal alignment
	plt.text(x + 0.04, y - 0.05, '%.2f'% y, ha='center', va='top')

# set range
plt.xlim((-.5, n))
plt.ylim((-1.25, 1.25))
# set tick none
plt.xticks(())
plt.yticks(())
plt.show()

13. Contours 等高线图

14. Image 图片

15. 3D 数据

16. Embedded Imaging 图中图

import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec

fig = plt.figure()
x = [1, 2, 3, 4, 5, 6, 7]
y = [1, 3, 4, 2, 9, 5, 8]

left, bottom, width, height = 0.1, 0.1, 0.8, 0.8
ax1 = fig.add_axes([left, bottom, width, height])
ax1.plot(x,y, 'r')
ax1.set_xlabel('x')
ax1.set_ylabel('y')
ax1.set_title("title")

left, bottom, width, height = 0.2, 0.6, 0.25, 0.25
ax2 = fig.add_axes([left, bottom, width, height])
ax2.plot(y,x, 'b')
ax2.set_xlabel('x')
ax2.set_ylabel('y')
ax2.set_title("inside 1")


plt.axes([0.6, 0.2, 0.25, 0.25])
plt.plot(y[::-1], x, 'g')
plt.xlabel('x')
plt.ylabel('y')
plt.title("inside 2")

plt.show()

17. Animation 动画

Table of Contents