绘制堆叠条形图
plotting stacked bar graph
我想使用 matplotlib 和 pandas.The 绘制堆叠条形图,当我更改 a,b,c,d,e,f,g,h 时,下面的代码非常 nicely.However 绘制条形图。 .etc 到 1 月、2 月...它不绘制相同的 graph.It 只绘制字母 order.Are 无论如何要克服这个问题。
import pandas as pd
import matplotlib.pyplot as plt
years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
"a":[20,0,0,0,10,21],
"b":[20,13,10,18,15,45],
"c":[20,20,10,15,18,78],
"d":[20,20,10,15,18,75],
"e":[20,20,10,15,18,78],
"f":[20,20,10,15,18,78],
"g":[20,20,10,15,18,78],
"h":[20,20,10,15,18,78],
"i":[20,20,10,15,18,78],
"j":[20,20,10,15,18,78],
"k":[20,20,10,15,18,78],
"l":[20,20,0,0,0,20],
}
df=pd.DataFrame(dataavail,index=years)
df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()
但是当我将 a、b、c 部分的代码...更改为一月、二月...时,它绘制的并不相同。
导入 pandas 作为 pd
将 matplotlib.pyplot 导入为 plt
years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
"january":[20,0,0,0,10,21],
"February":[20,13,10,18,15,45],
"March":[20,20,10,15,18,78],
"April":[20,20,10,15,18,75],
"may":[20,20,10,15,18,78],
"June":[20,20,10,15,18,78],
"July":[20,20,10,15,18,78],
"August":[20,20,10,15,18,78],
"September":[20,20,10,15,18,78],
"October":[20,20,10,15,18,78],
"November":[20,20,10,15,18,78],
"December":[20,20,0,0,0,20],
}
df=pd.DataFrame(dataavail,index=years)
df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()
在 Python 3.9.7 中,您的图形看起来是一样的:
>>> df_alpha
a b c d e f g h i j k l
2016 20 20 20 20 20 20 20 20 20 20 20 20
2017 0 13 20 20 20 20 20 20 20 20 20 20
2018 0 10 10 10 10 10 10 10 10 10 10 0
2019 0 18 15 15 15 15 15 15 15 15 15 0
2020 10 15 18 18 18 18 18 18 18 18 18 0
2021 21 45 78 75 78 78 78 78 78 78 78 20
>>> df_month
January February March April may June July August September October November December
2016 20 20 20 20 20 20 20 20 20 20 20 20
2017 0 13 20 20 20 20 20 20 20 20 20 20
2018 0 10 10 10 10 10 10 10 10 10 10 0
2019 0 18 15 15 15 15 15 15 15 15 15 0
2020 10 15 18 18 18 18 18 18 18 18 18 0
2021 21 45 78 75 78 78 78 78 78 78 78 20
完整代码:
import pandas as pd
import matplotlib.pyplot as plt
years = ['2016', '2017', '2018', '2019', '2020', '2021']
dataavail1 = {'a': [20, 0, 0, 0, 10, 21], 'b': [20, 13, 10, 18, 15, 45], 'c': [20, 20, 10, 15, 18, 78], 'd': [20, 20, 10, 15, 18, 75], 'e': [20, 20, 10, 15, 18, 78], 'f': [20, 20, 10, 15, 18, 78], 'g': [20, 20, 10, 15, 18, 78], 'h': [20, 20, 10, 15, 18, 78], 'i': [20, 20, 10, 15, 18, 78], 'j': [20, 20, 10, 15, 18, 78], 'k': [20, 20, 10, 15, 18, 78], 'l': [20, 20, 0, 0, 0, 20]}
dataavail2 = {'January': [20, 0, 0, 0, 10, 21], 'February': [20, 13, 10, 18, 15, 45], 'March': [20, 20, 10, 15, 18, 78], 'April': [20, 20, 10, 15, 18, 75], 'may': [20, 20, 10, 15, 18, 78], 'June': [20, 20, 10, 15, 18, 78], 'July': [20, 20, 10, 15, 18, 78], 'August': [20, 20, 10, 15, 18, 78], 'September': [20, 20, 10, 15, 18, 78], 'October': [20, 20, 10, 15, 18, 78], 'November': [20, 20, 10, 15, 18, 78], 'December': [20, 20, 0, 0, 0, 20]}
df_alpha = pd.DataFrame(dataavail1, index=years)
df_month = pd.DataFrame(dataavail2, index=years)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 8))
df_alpha.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax1, rot=0)
df_month.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax2, rot=0)
plt.show()
更新:代码也适用于Python 3.7.12
我想使用 matplotlib 和 pandas.The 绘制堆叠条形图,当我更改 a,b,c,d,e,f,g,h 时,下面的代码非常 nicely.However 绘制条形图。 .etc 到 1 月、2 月...它不绘制相同的 graph.It 只绘制字母 order.Are 无论如何要克服这个问题。
import pandas as pd
import matplotlib.pyplot as plt
years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
"a":[20,0,0,0,10,21],
"b":[20,13,10,18,15,45],
"c":[20,20,10,15,18,78],
"d":[20,20,10,15,18,75],
"e":[20,20,10,15,18,78],
"f":[20,20,10,15,18,78],
"g":[20,20,10,15,18,78],
"h":[20,20,10,15,18,78],
"i":[20,20,10,15,18,78],
"j":[20,20,10,15,18,78],
"k":[20,20,10,15,18,78],
"l":[20,20,0,0,0,20],
}
df=pd.DataFrame(dataavail,index=years)
df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()
但是当我将 a、b、c 部分的代码...更改为一月、二月...时,它绘制的并不相同。
导入 pandas 作为 pd 将 matplotlib.pyplot 导入为 plt
years=["2016","2017","2018","2019","2020", "2021"]
dataavail={
"january":[20,0,0,0,10,21],
"February":[20,13,10,18,15,45],
"March":[20,20,10,15,18,78],
"April":[20,20,10,15,18,75],
"may":[20,20,10,15,18,78],
"June":[20,20,10,15,18,78],
"July":[20,20,10,15,18,78],
"August":[20,20,10,15,18,78],
"September":[20,20,10,15,18,78],
"October":[20,20,10,15,18,78],
"November":[20,20,10,15,18,78],
"December":[20,20,0,0,0,20],
}
df=pd.DataFrame(dataavail,index=years)
df.plot(kind="bar",stacked=True,figsize=(10,8))
plt.legend(loc="centre",bbox_to_anchor=(0.8,1.0))
plt.show()
在 Python 3.9.7 中,您的图形看起来是一样的:
>>> df_alpha
a b c d e f g h i j k l
2016 20 20 20 20 20 20 20 20 20 20 20 20
2017 0 13 20 20 20 20 20 20 20 20 20 20
2018 0 10 10 10 10 10 10 10 10 10 10 0
2019 0 18 15 15 15 15 15 15 15 15 15 0
2020 10 15 18 18 18 18 18 18 18 18 18 0
2021 21 45 78 75 78 78 78 78 78 78 78 20
>>> df_month
January February March April may June July August September October November December
2016 20 20 20 20 20 20 20 20 20 20 20 20
2017 0 13 20 20 20 20 20 20 20 20 20 20
2018 0 10 10 10 10 10 10 10 10 10 10 0
2019 0 18 15 15 15 15 15 15 15 15 15 0
2020 10 15 18 18 18 18 18 18 18 18 18 0
2021 21 45 78 75 78 78 78 78 78 78 78 20
完整代码:
import pandas as pd
import matplotlib.pyplot as plt
years = ['2016', '2017', '2018', '2019', '2020', '2021']
dataavail1 = {'a': [20, 0, 0, 0, 10, 21], 'b': [20, 13, 10, 18, 15, 45], 'c': [20, 20, 10, 15, 18, 78], 'd': [20, 20, 10, 15, 18, 75], 'e': [20, 20, 10, 15, 18, 78], 'f': [20, 20, 10, 15, 18, 78], 'g': [20, 20, 10, 15, 18, 78], 'h': [20, 20, 10, 15, 18, 78], 'i': [20, 20, 10, 15, 18, 78], 'j': [20, 20, 10, 15, 18, 78], 'k': [20, 20, 10, 15, 18, 78], 'l': [20, 20, 0, 0, 0, 20]}
dataavail2 = {'January': [20, 0, 0, 0, 10, 21], 'February': [20, 13, 10, 18, 15, 45], 'March': [20, 20, 10, 15, 18, 78], 'April': [20, 20, 10, 15, 18, 75], 'may': [20, 20, 10, 15, 18, 78], 'June': [20, 20, 10, 15, 18, 78], 'July': [20, 20, 10, 15, 18, 78], 'August': [20, 20, 10, 15, 18, 78], 'September': [20, 20, 10, 15, 18, 78], 'October': [20, 20, 10, 15, 18, 78], 'November': [20, 20, 10, 15, 18, 78], 'December': [20, 20, 0, 0, 0, 20]}
df_alpha = pd.DataFrame(dataavail1, index=years)
df_month = pd.DataFrame(dataavail2, index=years)
fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 8))
df_alpha.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax1, rot=0)
df_month.plot(kind='bar', stacked=True, colormap=plt.cm.tab20, ax=ax2, rot=0)
plt.show()
更新:代码也适用于Python 3.7.12