Python Matplotlib库

2021-02-28  本文已影响0人  watermountain

Matplotlib是Python的绘图库。

可以和NumPy、图形库(PyQt、wxPython)配合使用。

示例代码及绘制图形如下:

# 引入numpy、matplotlib.pyplot库
import numpy as np
import matplotlib.pyplot as plt

# np.arange()函数创建数值范围并返回ndarray对象。
x = np.arange(1,11)
y = 2 * x + 5
plt.title("matplotlib eg1")
plt.xlabel('x axis')
plt.ylabel('y axis')
plt.plot(x, y)
plt.show()
matplotlib_Figure_1.png

subplot()函数

在同一图中绘制不同的内容

bar()函数

生成条形图

from matplotlib import pyplot as plt
x =  [5,8,10] 
y =  [12,16,6] 
x2 =  [6,9,11] 
y2 =  [6,15,7] 
plt.title('Bar graph')
plt.bar(x, y, align='center')
plt.bar(x2, y2, color='g', align='center')
plt.xlabel('X')
plt.ylabel('Y')
plt.show()
matplotlib_bar_Figure_1.png

numpy.histogram()

是数据的频率分布的图形表示。
水平尺寸相同的矩阵对应于类间隔,称为bin,变量height对应于频率。
numpy.histogram()将输入数组和bin作为两个参数。
bin数组中的连续元素用作每个bin的边界。

import numpy as np
array = np.array([22,87,5,43,56,73,55,54,11,20,51,5,79,31,27])
np.histogram(array, bins=[0,20,40,60,80,100])
hist, bins = np.histogram(array, bins=[0,20,40,60,80,100])
plt.hist(array, bins)
matplotlib_histogram_Figure_1.png
上一篇 下一篇

猜你喜欢

热点阅读