Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/347.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 带条的镶嵌面条形图在大熊猫中并排显示_Python_Pandas_Matplotlib_Plot_Facet - Fatal编程技术网

Python 带条的镶嵌面条形图在大熊猫中并排显示

Python 带条的镶嵌面条形图在大熊猫中并排显示,python,pandas,matplotlib,plot,facet,Python,Pandas,Matplotlib,Plot,Facet,这是可行的,但是有没有更好的方法来刻面条形图,在熊猫中,条形图是并排的?我觉得可能有一种干净的方法可以使用多索引和子地块参数进行编写 import pandas as pd import matplotlib.pyplot as plt df = pd.DataFrame({'group': ['A', 'A', 'B', 'B'], 'name': ['name1', 'name2', 'name3', 'name4'],

这是可行的,但是有没有更好的方法来刻面条形图,在熊猫中,条形图是并排的?我觉得可能有一种干净的方法可以使用多索引和子地块参数进行编写

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'group': ['A', 'A', 'B', 'B'], 
                   'name': ['name1', 'name2', 'name3', 'name4'], 
                   '2016': [1, 2, 3, 4], 
                   '2017': [1.2, 2.1, 3.0, 4.9]})    
df.set_index(['name'], inplace=True)

fig, ax = plt.subplots(2)

group_a = df[df.group == 'A']
group_b = df[df.group == 'B']
group_a.plot(kind='bar', rot=0, ax=ax[0])
group_b.plot(kind='bar', rot=0, ax=ax[1])

如果你想做不止一次的事情,总是值得考虑使用函数和循环。在这里,循环就足够了

groups = df.group.unique()

fig, ax = plt.subplots(len(groups))

for i, group in enumerate(groups):
    df[df.group == group].plot(kind='bar', rot=0, ax=ax[i])