Python | Matplotlib简介

Matplotlib是Python中一个惊人的可视化库,用于阵列的二维绘图。Matplotlib是一个基于NumPy阵列的多平台数据可视化库,旨在与更广泛的SciPy堆栈配合使用。它是由约翰·亨特在2002年推出的。

null

可视化的最大好处之一是,它允许我们以易于理解的视觉形式直观地访问大量数据。Matplotlib由线、条、散点、直方图等几个图组成。

安装: Windows、Linux和macOS发行版都有matplotlib及其大部分依赖项,它们都是轮子包。运行以下命令以安装 matplotlib 包裹:

python -mpip install -U matplotlib

导入matplotlib:

from matplotlib import pyplot as plt
or
import matplotlib.pyplot as plt 

Matplotlib中的基本绘图:

Matplotlib有各种各样的情节。曲线图有助于了解趋势、模式,并进行相关性分析。它们通常是对定量信息进行推理的工具。这里介绍了一些样地。

线形图:

# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [ 5 , 2 , 9 , 4 , 7 ]
# Y-axis values
y = [ 10 , 5 , 8 , 4 , 2 ]
# Function to plot
plt.plot(x,y)
# function to show the plot
plt.show()


输出:

图片[1]-Python | Matplotlib简介-yiteyi-C++库 条形图:

# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [ 5 , 2 , 9 , 4 , 7 ]
# Y-axis values
y = [ 10 , 5 , 8 , 4 , 2 ]
# Function to plot the bar
plt.bar(x,y)
# function to show the plot
plt.show()


输出: 图片[2]-Python | Matplotlib简介-yiteyi-C++库 直方图:

# importing matplotlib module
from matplotlib import pyplot as plt
# Y-axis values
y = [ 10 , 5 , 8 , 4 , 2 ]
# Function to plot histogram
plt.hist(y)
# Function to show the plot
plt.show()


输出:

图片[3]-Python | Matplotlib简介-yiteyi-C++库

散点图:

# importing matplotlib module
from matplotlib import pyplot as plt
# x-axis values
x = [ 5 , 2 , 9 , 4 , 7 ]
# Y-axis values
y = [ 10 , 5 , 8 , 4 , 2 ]
# Function to plot scatter
plt.scatter(x, y)
# function to show the plot
plt.show()


输出:

图片[4]-Python | Matplotlib简介-yiteyi-C++库 参考: Matplotlib文档 .

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享