🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## matplotlib * 1 随机显示50个散点图 * 随机50个点信息 * 绘制散点图 * 显示 ``` import numpy as np # 导入绘图工具包 import matplotlib.pyplot as plt N = 50 x = np.random.rand(N) y = np.random.rand(N) colors = np.random.rand(N) r = 15*np.random.rand(N) area = np.pi *r**2 # 绘制散点图 plt.scatter(x, y, s=area, c=colors, alpha=0.5) plt.show() ``` * 绘图工具中,对象列表 * Figure 对象,画布 * Axes 对象,坐标轴 * Line2D 对象,线条 * Text 对象,文字 * 2 创建画布和坐标轴 * 创建画布 * 画布上面创建坐标轴 * 获得画布 * 获得坐标轴 * 创建画布2 坐标轴2 * 获得画布2 ``` # 创建画布 fig1 = plt.figure() # 创建坐标轴 # 坐标轴在画布上的布局 ax1 = fig1.add_subplot(1, 1, 1) fig1 # 获得画布 plt.gcf() ax1 # 获得坐标轴 plt.gca() fig2 = plt.figure() ax2 = fig2.add_subplot(1, 1, 1) fig2 plt.gcf() ``` * 3 创建画布绘制图像 * 创建画布 * 创建三个坐标轴 * 坐标轴1 线形图 * 坐标轴2 直方图 * 坐标轴3 散点图 ``` import numpy as np import matplotlib.pyplot as plt # 如果报错添加 import matplotlib matplotlib.use('TkAgg') fig = plt.figure() ax1 = fig.add_subplot(2, 2, 1) ax2 = fig.add_subplot(2, 2, 2) ax3 = fig.add_subplot(2, 2, 3) # 线形图 ax1.plot(np.random.randn(50).cumsum(), 'k--') # 直方图 _ = ax2.hist(np.random.randn(100), bins=20, color='k') # 散点图 ax3.scatter(np.arange(30), np.arange(30) + 3 * np.random.randn(30)) fig.show() ``` ### 常用属性设置 * 4 坐标轴属性设置 * 创建画布 * 创建坐标轴 * 设置标题 * 设置刻度 * 设置x y轴标签 * 设置网格 * 添加文本 ``` import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1,1,1) # 设置标题 ax.set_title("Axes Example") major_ticks = np.arange(0, 101, 20) minor_ticks = np.arange(0, 101, 5) # 设置刻度 ax.set_xticks(major_ticks) ax.set_xticks(minor_ticks, minor=True) ax.set_yticks(major_ticks) ax.set_yticks(minor_ticks, minor=True) # 设置 X, Y 轴 标签 ax.set_xlabel("X axis") ax.set_ylabel("Y axis") # 设置网格 ax.grid(which='minor', alpha=0.2) ax.grid(which='major', alpha=0.5) # 添加文字 ax.text(42.5, 50, "viviwong") fig.show() ``` * 5 绘制曲线颜色 * 绘制五根线形图 ``` import numpy as np import matplotlib.pyplot as plt # 0-1 平均创建100个值 x = np.linspace(0, 1,100) fig = plt.figure() ax = fig.add_subplot(1,1,1) ax.set_title("viviwong") ax.plot(x, x ** (1/8), 'b--', label=r'$y = x^{1/8}$') ax.plot(x, x ** 8, 'r--', label=r'$y = x^{8}$') ax.plot(x, x ** (1/2), 'r.', label=r'$y = x^{1/2}$') ax.plot(x, x ** 2, 'b.', label=r'$y = x^{2}$') ax.plot(x, x, 'g-', label=r'$y = x$') ax.legend() ax.axis([0, 1, 0, 1]) fig.show() ``` ### 常用图形 #### 线形图 * 6 线形图 * 创建画布 创建坐标轴 * 随机100个点 * 坐标轴上绘制线形图 坐标轴.plot(x,y) * 显示画布 ``` import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1,1,1) x = np.random.randn(100) y = np.random.randn(100) ax.plot(x, y) fig.show() ``` #### 直方图 * 7 画直方图 * 创建画布 创建坐标轴 * 产生1000个数值 * 创建刻度 * 绘制直方图 * 显示画布 ``` import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1,1,1) # 产生1000个正态分布数值 data = np.random.normal(0, 20, 1000) # 创建刻度 bins = np.arange(-100, 100, 5) # 绘制直方图 ax.hist(data, bins=bins) fig.show() ``` #### 散点图 * 8 绘制散点图 ``` import numpy as np import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(1,1,1) x = np.arange(1, 101) y = 20 + 3 * x + np.random.normal(0, 60, 100) ax.scatter(x, y) fig.show() ``` ## Pandas 绘图 * 9 Pandas绘制线图 ``` import matplotlib.pyplot as plt import numpy as np import pandas as pd from pandas import Series, DataFrame s = Series(np.random.randn(10).cumsum(), index=np.arange(0, 100, 10)) # 绘制线形图 s.plot() plt.show() ```