21、numpy—Matplotlib
NumPy MatplotlibMatplotlib 是 Python 的绘图库。 它可与 NumPy 一起使用,提供了一种有效的 MatLab 开源替代方案。 它也可以和图形工具包一起使用,如 PyQt 和 wxPython。 引例:np.arange() 函数创建 x 轴上的值。y 轴上的对应值存储在另一个数组对象 y 中。 这些值使用 matplotlib 软件包的 pyplot 子模块的 plot() 函数绘制。1 import numpy as np 2 from matplotlib import pyplot as plt 3 x = np.arange(1,11) 4 y = 2 * x + 5 5 print(‘x:{}ty:{}‘.format(x,y)) 6 plt.title("Matplotlib demo") 7 plt.xlabel("x axis caption") 8 plt.ylabel("y axis caption") 9 plt.plot(x,y) 10 plt.show() 执行结果: x:[ 1 2 3 4 5 6 7 8 9 10] y:[ 7 9 11 13 15 17 19 21 23 25] ?图形由 show() 函数显示 1、中文显示问题打印系统字体: 1 import matplotlib 2 a = sorted([f.name for f in matplotlib.font_manager.fontManager.ttflist]) 3 for i in a: 4 print(i) 执行结果: 打印出你的 font_manager 的 ttflist 中所有注册的名字,找一个看中文字体例如:STFangsong(仿宋),然后添加以下代码即可: plt.rcParams[‘font.family‘]=[‘STFangsong‘] 例子: 1 import numpy as np 2 from matplotlib import pyplot as plt 3 plt.rcParams[‘font.family‘] = [‘STFangsong‘] 4 x = np.arange(1,11) 5 y = 2 * x + 5 6 plt.title("中文- 测试") 7 plt.xlabel("x 轴") 8 plt.ylabel("y 轴") 9 plt.plot(x,y) 10 plt.show() 执行结果: ?2、离散图设置和颜色设置作为线性图的替代,可以通过向 plot() 函数添加格式字符串来显示离散值。 可以使用以下格式化字符。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?以下是颜色的缩写:
? ? ? ? ? ? ? ? 比如我们要显示蓝色的圆来代表点,我们可以使用‘ob‘作为plot()函数的格式字符串: 将plt.plot(x,y)改为 plt.plot(x,y,‘ob‘) ? 我们可以得到输出图: ? ?3、绘制正弦波?以下实例使用 matplotlib 生成正弦波图。 1 import numpy as np 2 import matplotlib.pyplot as plt 3 plt.rcParams[‘font.family‘] = [‘STFangsong‘] 4 x = np.arange(0,4*np.pi,0.1) 5 y = np.sin(x) 6 plt.title(‘正弦波绘制‘) 7 plt.xlabel(‘x轴‘) 8 plt.ylabel(‘y轴‘) 9 plt.plot(x,‘r‘) 10 plt.show() ? ?执行结果: ? ?4、subplot() 函数允许你在同一图中绘制不同的东西。?以下实例绘制正弦和余弦值: import numpy as np import matplotlib.pyplot as plt # 计算正弦和余弦曲线上的点的 x 和 y 坐标 x = np.arange(0,4 * np.pi,0.1) y_sin = np.sin(x) y_cos = np.cos(x) # 建立 subplot 网格,高为 2,宽为 1 # 激活第一个 subplot plt.subplot(2,1,1) # 绘制第一个图像 plt.plot(x,y_sin) plt.title(‘Sine‘) # 将第二个 subplot 激活,并绘制第二个图像 plt.subplot(2,2) plt.plot(x,y_cos) plt.title(‘Cosine‘) # 展示图像 plt.show() ? ?执行输出结果: 5、Matplotlib中柱状图bar使用? pyplot 子模块提供 bar() 函数来生成条形图。 以下实例生成两组 x 和 y 数组的条形图。 1 from matplotlib import pyplot as plt 2 x = [5,8,10] 3 y = [12,16,6] 4 x2 = [6,9,11] 5 y2 = [6,15,7] 6 plt.bar(x,align = ‘center‘) 7 plt.bar(x2,y2,color = ‘g‘,align = ‘center‘) 8 plt.title(‘Bar graph‘) 9 plt.ylabel(‘Y axis‘) 10 plt.xlabel(‘X axis‘) 11 plt.show() ? 执行结果: ? ?6、直方图numpy.histogram()numpy.histogram() 函数是数据的频率分布的图形表示。 水平尺寸相等的矩形对应于类间隔,称为 bin,变量 height 对应于频率。 numpy.histogram()函数将输入数组和 bin 作为两个参数。 bin 数组中的连续元素用作每个 bin 的边界。 1 import numpy as np 2 3 a = np.array([22,87,5,43,56,73,55,54,11,20,51,79,31,27]) 4 np.histogram(a,bins = [0,40,60,80,100]) 5 hist,bins = np.histogram(a,100]) 6 print (hist) 7 print (bins) ? 执行结果: [3 4 5 2 1]
[ 0 20 40 60 80 100]
plt()Matplotlib 可以将直方图的数字表示转换为图形。 pyplot 子模块的 plt() 函数将包含数据和 bin 数组的数组作为参数,并转换为直方图。 1 import numpy as np 2 import matplotlib.pyplot as plt 3 4 a = np.array([22,27]) 5 np.histogram(a,bins=[0,100]) 6 hist,100]) 7 print(hist) 8 print(bins) 9 plt.hist(a,bins) 10 plt.show() ? 执行结果: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |