- The simplest way is to create a dataframe with pandas, and then plot with pandas.DataFrame.plot https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html
- 数据帧索引,
'names'
在本例中,自动使用 x 轴,并将列绘制为条形。
-
matplotlib
用作绘图后端
- 测试于
python 3.8
, pandas 1.3.1
and matplotlib 3.4.2
- 对于长度不均匀的列表,请参见如何从不均匀长度的列表创建分组条形图 https://stackoverflow.com/a/62480170/7758804
import pandas as pd
import matplotlib.pyplot as plt
names = ["a","b","c","d"]
case1 = [5,7,5,6]
case2 = [7,4,8,5]
# create the dataframe
df = pd.DataFrame({'c1': case1, 'c2': case2}, index=names)
# display(df)
c1 c2
a 5 7
b 7 4
c 5 8
d 6 5
# plot
ax = df.plot(kind='bar', figsize=(6, 4), rot=0, title='Case Comparison', ylabel='Values')
plt.show()
fig, ax = plt.subplots(figsize=(6, 4))
df.plot.bar(ax=ax, rot=0)
ax.set(ylabel='Values')
plt.show()