赞
踩
1、散点图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# scatter 建立一个表格
fig = plt.figure()
ax = fig.add_subplot(3, 3, 1) # 3行3列的第一个位置
n = 128
X = np.random.normal(0, 1, n) # 生成随机数
Y = np.random.normal(0, 1, n)
T = np.arctan2(Y, X) # 上颜色
# plt.axes([0.025, 0.025, 0.95, 0.95]) # 坐标的范围
ax.scatter(X, Y, s=75, c=T, alpha=0.5) # s表示的是点的大小,c表示的是点的颜色,alpha透明度
plt.xlim(-1.5, 1.5), plt.xticks([]) # x的范围
plt.ylim(-1.5, 1.5), plt.yticks([])
plt.axis()
plt.title("scatter")
plt.xlabel("x")
plt.ylabel("y")
plt.show()
if __name__ == "__main__":
main()
结果为:
2、条形图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# bar
fig = plt.figure()
ax = fig.add_subplot(332) # 添加变量ax的目的是为了让图在子图上画,其实这里的子图plt也是
n = 10
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)
ax.bar(X, +Y1, facecolor='#9999ff', edgecolor='white') # +Y1 放在上面
ax.bar(X, -Y2, facecolor='#ff9999', edgecolor='white')
for x, y in zip(X, Y1): # 添加注释
plt.text(x + 0.4, y + 0.05, '%0.2f' % y, ha='center', va='bottom')
for x, y in zip(X, Y2): # 前两个表示位置,后两个表示格式. bottom和top可改变坐标轴的位置
plt.text(x + 0.4, -y - 0.05, '%0.2f' % y, ha='center', va='top')
plt.show()
if __name__ == "__main__":
main()
结果为:
3、饼图
import matplotlib.pyplot as plt
import numpy as np
def main():
# pie 饼图
fig = plt.figure()
fig.add_subplot(333)
n = 20
Z = np.ones(n)
Z[-1] *= 2
plt.pie(Z, explode=Z * 0.05, colors=['%f' % (i/float(n)) for i in range(n)],
labels=['%0.2f' % (i/float(n)) for i in range(n)])
# Z 代表每一块的值,explode每一个扇形离中心的距离
plt.gca().set_aspect('equal') # set_aspect('equal')的意思是让他成为正的圆形,不能是椭圆
plt.xticks([]), plt.yticks([])
plt.show()
if __name__ == "__main__":
main()
结果为:
4、极值图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# polar 极值图
fig = plt.figure()
fig.add_subplot(334, polar=True) # 如果没有参数polar=True,画出来的图仅仅只是折线
n = 20
theta = np.arange(0.0, 2 * np.pi, 2 * np.pi/n)
radii = 10 * np.random.rand(n) # 半径
plt.plot(theta, radii)
plt.show()
plt.show()
if __name__ == "__main__":
main()
结果为:
5、热图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# heatmap 热图
fig = plt.figure()
fig.add_subplot(335)
from matplotlib import cm # 上色用的
data = np.random.rand(3, 3)
cmap = cm.Blues
map = plt.imshow(data, interpolation='nearest', cmap=cmap, aspect='auto', vmin=0, vmax=1)
# 这里的插值用的是最近插值的方法
plt.show()
if __name__ == "__main__":
main()
结果为:
6、3D图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# 3D 图
fig = plt.figure()
from mpl_toolkits.mplot3d import Axes3D # 引入三维坐标系
ax = fig.add_subplot(336, projection="3d")
ax.scatter(1, 1, 3, s=100) # 画一个大大的点
plt.show()
if __name__ == "__main__":
main()
结果为:
7、热力图:
import matplotlib.pyplot as plt
import numpy as np
def main():
# hot map 热图
fig = plt.figure()
fig.add_subplot(313)
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.contourf(X, Y, f(X, Y), 8, alpha=0.75, cmap=plt.cm.hot)
plt.show()
if __name__ == "__main__":
main()
结果为:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。