PythonMatplotlib教程展示了如何使用Matplotlib在Python中创建图表。我们创建了散点图、折线图、条形图和饼图。
Matplotlib
Matplotlib是一个用于创建图表的Python库。Matplotlib可用于Python脚本、Python和IPythonshell、jupyternotebook、web应用程序服务器和四个图形用户界面工具包。
Matplotlib安装
Matplotlib是一个需要安装的外部Python库。
$ sudo pip install matplotlib
我们可以使用pip工具来安装这个库。
Matplotlib散点图
散点图是一种绘图或数学图表,使用笛卡尔坐标来显示一组数据的通常两个变量的值。
#!/usr/bin/python
import matplotlib.pyplot as plt
x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_axis = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89]
plt.title("Prices over 10 years")
plt.scatter(x_axis, y_axis, color='darkblue', marker='x', label="item 1")
plt.xlabel("Time (years)")
plt.ylabel("Price (dollars)")
plt.grid(True)
plt.legend()
plt.show()
示例绘制散点图。该图表显示了十年间某些商品的价格。
import matplotlib.pyplot as plt
我们从matplotlib模块导入pyplot。它是创建图表的命令样式函数的集合。操作上与MATLAB类似。
x_axis = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89]
我们有x轴和y轴的数据。
plt.title("Prices over 10 years")
使用title函数,我们为图表设置标题。
plt.scatter(x_axis, y_axis, color='darkblue', marker='x', label="item 1")
scatter函数绘制散点图。它接受x轴和y轴的数据、标记的颜色、标记的形状和标签。
plt.xlabel("Time (years)")
plt.ylabel("Price (dollars)")
我们为轴设置标签。
plt.grid(True)
我们使用grid函数显示网格。网格由许多垂直线和水平线组成。
plt.legend()
legend函数在坐标轴上放置图例。
plt.show()
show函数显示图表。
Mathplotlib两个数据集
在下一个示例中,我们向图表添加另一个数据集。
#!/usr/bin/python
import matplotlib.pyplot as plt
x_axis1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_axis1 = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89]
x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
y_axis2 = [53, 6, 46, 36, 15, 64, 73, 25, 82, 9] 
plt.title("Prices over 10 years")
plt.scatter(x_axis1, y_axis1, color='darkblue', marker='x', label="item 1")
plt.scatter(x_axis2, y_axis2, color='darkred', marker='x', label="item 2")
plt.xlabel("Time (years)")
plt.ylabel("Price (dollars)")
plt.grid(True)
plt.legend()
plt.show()
图表显示两个数据集。我们通过标记的颜色来区分它们。
x_axis1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis1 = [5, 16, 34, 56, 32, 56, 32, 12, 76, 89] x_axis2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] y_axis2 = [53, 6, 46, 36, 15, 64, 73, 25, 82, 9]
我们有两个数据集。
plt.scatter(x_axis1, y_axis1, color='darkblue', marker='x', label="item 1") plt.scatter(x_axis2, y_axis2, color='darkred', marker='x', label="item 2")
我们为每个集合调用scatter函数。
Matplotlib折线图
折线图是一种将信息显示为一系列数据点的图表,这些数据点称为标记,由直线段连接。
#!/usr/bin/python
import numpy as np
import matplotlib.pyplot as plt
t = np.arange(0.0, 3.0, 0.01)
s = np.sin(2.5 * np.pi * t)
plt.plot(t, s)
 
plt.xlabel('time (s)')
plt.ylabel('voltage (mV)')
plt.title('Sine Wave')
plt.grid(True)
plt.show()
该示例显示正弦波折线图。
import numpy as np
在示例中,我们还需要numpy模块。
t = np.arange(0.0, 3.0, 0.01)
arange函数返回给定区间内均匀分布的值列表。
s = np.sin(2.5 * np.pi * t)
我们得到数据的sin值。
plt.plot(t, s)
我们用plot函数绘制折线图。
Matplotlib条形图
条形图使用矩形条显示分组数据,矩形条的长度与其表示的值成正比。条形图可以垂直或水平绘制。
#!/usr/bin/python
from matplotlib import pyplot as plt
from matplotlib import style
style.use('ggplot')
x = [0, 1, 2, 3, 4, 5]
y = [46, 38, 29, 22, 13, 11]
fig, ax = plt.subplots()
ax.bar(x, y, align='center')
ax.set_title('Olympic Gold medals in London')
ax.set_ylabel('Gold medals')
ax.set_xlabel('Countries')
ax.set_xticks(x)
ax.set_xticklabels(("USA", "China", "UK", "Russia", 
    "South Korea", "Germany"))
plt.show()
示例绘制条形图。它显示了每个国家在2012年伦敦奥运会上获得的奥运金牌数。
style.use('ggplot')
可以使用预定义的样式。
fig, ax = plt.subplots()
subplots函数返回一个图形和一个坐标区对象。
ax.bar(x, y, align='center')
条形图是用bar函数生成的。
ax.set_xticks(x)
ax.set_xticklabels(("USA", "China", "UK", "Russia", 
    "South Korea", "Germany"))
我们为x轴设置国家/地区名称。
Matplotlib饼图
饼图是一个圆形图表,它被分成多个部分以说明数字比例。
#!/usr/bin/python
import matplotlib.pyplot as plt
 
labels = ['Oranges', 'Pears', 'Plums', 'Blueberries']
quantity = [38, 45, 24, 10]
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
plt.pie(quantity, labels=labels, colors=colors, autopct='%1.1f%%', 
    shadow=True, startangle=90)
plt.axis('equal')
plt.show()
该示例创建了一个饼图。
labels = ['Oranges', 'Pears', 'Plums', 'Blueberries'] quantity = [38, 45, 24, 10]
我们有标签和相应的数量。
colors = ['yellowgreen', 'gold', 'lightskyblue', 'lightcoral']
我们为饼图的切片定义颜色。
plt.pie(quantity, labels=labels, colors=colors, autopct='%1.1f%%', 
    shadow=True, startangle=90)
饼图是用pie函数生成的。autopct负责在图表的楔形中显示百分比。
plt.axis('equal')
我们设置了一个相等的长宽比,这样饼图就画成了一个圆。
在本教程中,我们使用Matplotlib库创建了散点图、折线图、条形图和饼图。
列出所有Python教程。

