Python Pandas - 在一个 DataFrame 中绘制多条数据列?
要绘制多条列,我们将绘制一个条形图。使用 plot() 方法并将 kind 参数设置为 bar 以绘制条形图。让我们首先导入所需的库 -
import pandas as pd import matplotlib.pyplot as mp
下面是我们的球队记录数据 -
data = [["Australia", 2500, 2021],["Bangladesh", 1000, 2021],["England", 2000, 2021],["India", 3000, 2021],["Srilanka", 1500, 2021]]
将数据设置为 Pandas DataFrame 并添加列 -
dataFrame = pd.DataFrame(data, columns=["Team","Rank_Points", "Year"])
在条形图中绘制多条列。我们为此将“kind”参数设置为“bar” -
dataFrame.plot(x="Team", y=["Rank_Points","Year" ], kind="bar", figsize=(10, 9))
示例
以下是代码 -
import pandas as pd import matplotlib.pyplot as mp # our data data = [["Australia", 2500, 2021],["Bangladesh", 1000, 2021],["England", 2000, 2021],["India", 3000, 2021],["Srilanka", 1500, 2021]] # dataframe dataFrame = pd.DataFrame(data, columns=["Team","Rank_Points", "Year"]) # plotting multiple columns in a bar Graph dataFrame.plot(x="Team", y=["Rank_Points","Year" ], kind="bar", figsize=(10, 9)) # displaying bar graph mp.show()
输出
这将生成以下输出 -
广告