1.折线图在绘制折线图时,如果你的数据很小,图表的线条有点折,当你数据集比较大时候,比如超过100个点,则会呈现相对平滑的曲线。 在这里,我们使用三个plt.plot绘制了,不同斜率(1,2和3)的三条线。 import numpy as np import matplotlib.pyplot as plt cc= np.linspace(0,2,100) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.plot(cc,cc,label='linear') plt.plot(cc,cc**2,label='两倍') plt.plot(cc,cc**3,label='三倍') plt.xlabel('x label') plt.ylabel('y label') plt.title('折线图') plt.legend() plt.show() cc = np.linspace(0,2,100) plt.plot(cc,cc,label ='linear') plt.plot(cc,cc ** 2,label ='quadratic') plt.plot(cc,cc ** 3,label ='cubic') plt.xlabel('x label') plt.ylabel('y label') 结果显示,如下: 注意为了显示中文,我们plt.rcParams属性设置了中文字体,不然不能正确显示中文title的。 3.直方图直方图也是一种常用的简单图表,本例中我们在同一张图片中绘制两个概率直方图。 import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) mu1, sigma1 = 100, 15 mu2, sigma2 = 80, 15 x1 = mu1 + sigma1 * np.random.randn(10000) x2 = mu2 + sigma2 * np.random.randn(10000) n1, bins1, patches1 = plt.hist(x1, 50, density=True, facecolor='g', alpha=1) n2, bins2, patches2 = plt.hist(x2, 50, density=True, facecolor='r', alpha=0.2) plt.rcParams['font.sans-serif'] = ['SimHei'] plt.xlabel('智商') plt.ylabel('置信度') plt.title('IQ直方图') plt.text(110, .025, r'$mu=100, sigma=15$') plt.text(50, .025, r'$mu=80, sigma=15$') # 设置坐标范围 plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() 显示效果为: 4.条形图我们要介绍的第四种,图表类型是条形图,我们这儿引入稍微比较复杂的条形图。 4.1平行条形图此例中,我们引入三组(a,b,c)5个随机数(0~1),并用条形图打印出来,做比较 import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) total_width, n = 0.8, 3 width = total_width / n # redraw the coordinates of x x = x - (total_width - width) / 2 # here is the offset plt.bar(x, a, width=width, label='a') plt.bar(x + width, b, width=width, label='b') plt.bar(x + 2 * width, c, width=width, label='c') plt.legend() plt.show() 显示效果为: 4.2堆积条形图数据同上,不过条形plot的时候,用的相互的值大小差异(水平方向),而不是条柱平行对比。 import numpy as np import matplotlib.pyplot as plt size = 5 a = np.random.random(size) b = np.random.random(size) c = np.random.random(size) x = np.arange(size) plt.bar(x, a, width=0.5, label='a',fc='r') plt.bar(x, b, bottom=a, width=0.5, label='b', fc='g') plt.bar(x, c, bottom=a+b, width=0.5, label='c', fc='b') plt.ylim(0, 2.5) plt.legend() plt.grid(True) plt.show() 显示效果为: 5.2嵌套饼图import numpy as np import matplotlib.pyplot as plt size = 0.3 vals = np.array([[60., 32.], [37., 40.], [29., 10.]]) cmap = plt.get_cmap('tab20c') outer_colors = cmap(np.arange(3)*4) inner_colors = cmap(np.array([1, 2, 5, 6, 9, 10])) print(vals.sum(axis=1)) # [92. 77. 39.] plt.pie(vals.sum(axis=1), radius=1, colors=outer_colors, wedgeprops=dict(width=size, edgecolor='w')) print(vals.flatten()) # [60. 32. 37. 40. 29. 10.] plt.pie(vals.flatten(), radius=1-size, colors=inner_colors, wedgeprops=dict(width=size, edgecolor='w')) # equal makes it a perfect circle plt.axis('equal') plt.show() 显示效果为: 5.3极轴饼图极轴饼图是一种非常酷的图表,让我们看他的源码: import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) N = 10 theta = np.linspace(0.0, 2 * np.pi, N, endpoint=False) radii = 10 * np.random.rand(N) width = np.pi / 4 * np.random.rand(N) ax = plt.subplot(111, projection='polar') bars = ax.bar(theta, radii, width=width, bottom=0.0) for r, bar in zip(radii, bars): bar.set_facecolor(plt.cm.viridis(r / 10.)) bar.set_alpha(0.5) plt.show() 显示效果为: 6.3D图表3D图表也是能我们展示出超想象力的视觉效果的图表。 6.1三维散点图首先来看看三维的散点图,源码: import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D data = np.random.randint(0, 255, size=[40, 40, 40]) x, y, z = data[0], data[1], data[2] ax = plt.subplot(111, projection='3d') ax.scatter(x[:10], y[:10], z[:10], c='y') ax.scatter(x[10:20], y[10:20], z[10:20], c='r') ax.scatter(x[30:40], y[30:40], z[30:40], c='g') ax.set_zlabel('Z') ax.set_ylabel('Y') ax.set_xlabel('X') plt.show() 显示效果为: 怎么样,效果很酷把,好今天就给大家介绍到这里,如果你有任何问题,可以加到我的一个交流㪊:548+377+875 源码也有!教程也有!一起交流 快速入门! |
|