如何使用 Matplotlib 绘制 Pandas 数据框?
先决条件:
- 熊猫
- Matplotlib
数据可视化是任何分析中最重要的部分。 Matplotlib 是一个了不起的Python库,可用于绘制 Pandas 数据框。有多种方法可以根据要求生成绘图。
分类数据之间的比较
条形图就是这样一个例子。将使用 plot()函数绘制条形图。
句法:
matplotlib.pyplot.plot(\*args, scalex=True, scaley=True, data=None, \*\*kwargs)
例子:
Python3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'Name': ['John', 'Sammy', 'Joe'],
'Age': [45, 38, 90]
})
# plotting a bar graph
df.plot(x="Name", y="Age", kind="bar")
Python3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'Age': [45, 38, 90, 60, 40, 50, 2, 32, 8, 15, 27, 69, 73, 55]
})
# plotting a histogram
plt.hist(df["Age"])
plt.show()
Python3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'Object': ['Bulb', 'Lamp', 'Table', 'Pen', 'Notebook'],
'Price': [45, 38, 90, 60, 40]
})
# plotting a pie chart
plt.pie(df["Price"], labels=df["Object"])
plt.show()
Python3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'X': [1, 2, 3, 4, 5],
'Y': [2, 4, 6, 10, 15]
})
# plotting a line graph
print("Line graph: ")
plt.plot(df["X"], df["Y"])
plt.show()
# plotting a scatter plot
print("Scatter Plot: ")
plt.scatter(df["X"], df["Y"])
plt.show()
输出:
可视化连续数据
直方图是将数据表示为划分为密切相关的区间的示例。为此将使用 hist()函数。
句法:
matplotlib.pyplot.hist(x, bins=None, range=None, density=False, weights=None, cumulative=False, bottom=None, histtype=’bar’, align=’mid’, orientation=’vertical’, rwidth=None, log=False, color=None, label=None, stacked=False, \*, data=None, \*\*kwargs)
例子:
蟒蛇3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'Age': [45, 38, 90, 60, 40, 50, 2, 32, 8, 15, 27, 69, 73, 55]
})
# plotting a histogram
plt.hist(df["Age"])
plt.show()
输出:
用于数据分发
饼图是表示作为整体一部分的数据的好方法。要绘制饼图,将使用 pie()函数。
句法:
matplotlib.pyplot.pie(data, explode=None, labels=None, colors=None, autopct=None, shadow=False)
例子:
蟒蛇3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'Object': ['Bulb', 'Lamp', 'Table', 'Pen', 'Notebook'],
'Price': [45, 38, 90, 60, 40]
})
# plotting a pie chart
plt.pie(df["Price"], labels=df["Object"])
plt.show()
输出:
数据依赖
在根据相关和非相关参数解释数据的情况下,使用折线图或散点图等图形。绘制折线图 plot()函数就足够了,但使用 scatter() 来可视化散点图。
句法:
matplotlib.pyplot.scatter(x_axis_data, y_axis_data, s=None, c=None, marker=None, cmap=None, vmin=None, vmax=None, alpha=None, linewidths=None, edgecolors=None)
例子:
蟒蛇3
# importing pandas library
import pandas as pd
# importing matplotlib library
import matplotlib.pyplot as plt
# creating dataframe
df = pd.DataFrame({
'X': [1, 2, 3, 4, 5],
'Y': [2, 4, 6, 10, 15]
})
# plotting a line graph
print("Line graph: ")
plt.plot(df["X"], df["Y"])
plt.show()
# plotting a scatter plot
print("Scatter Plot: ")
plt.scatter(df["X"], df["Y"])
plt.show()
输出: