处理具有相同列和索引的多个数据帧时,可能需要创建集群堆积条形图以可视化数据。当您想要分别堆叠每个数据帧的条形图并按相应的索引分组时,就会出现挑战。
结合使用 Pandas 和 Matplotlib,我们可以实现这一目标通过手动调整条形矩形的位置和阴影图案。下面是详细的解决方案:
<code class="python">import pandas as pd import numpy as np import matplotlib.pyplot as plt def plot_clustered_stacked(dfall, labels=None, title="multiple stacked bar plot", H="/" , **kwargs): n_df = len(dfall) n_col = len(dfall[0].columns) n_ind = len(dfall[0].index) axe = plt.subplot(111) for df in dfall: # for each data frame axe = df.plot(kind="bar", linewidth=0, stacked=True, ax=axe, legend=False, grid=False, **kwargs) # make bar plots h, l = axe.get_legend_handles_labels() # get the handles we want to modify for i in range(0, n_df * n_col, n_col): # len(h) = n_col * n_df for j, pa in enumerate(h[i:i+n_col]): for rect in pa.patches: # for each index rect.set_x(rect.get_x() + 1 / float(n_df + 1) * i / float(n_col)) rect.set_hatch(H * int(i / n_col)) #edited part rect.set_width(1 / float(n_df + 1)) axe.set_xticks((np.arange(0, 2 * n_ind, 2) + 1 / float(n_df + 1)) / 2.) axe.set_xticklabels(df.index, rotation = 0) axe.set_title(title) # Add invisible data to add another legend n=[] for i in range(n_df): n.append(axe.bar(0, 0, color="gray", hatch=H * i)) l1 = axe.legend(h[:n_col], l[:n_col], loc=[1.01, 0.5]) if labels is not None: l2 = plt.legend(n, labels, loc=[1.01, 0.1]) axe.add_artist(l1) return axe</code>
使用 Seaborn 的 barplot 函数,我们可以创建堆叠条形图,但无法原生堆叠不同数据帧的条形图。为了克服这个问题,我们可以使用以下解决方法:
<code class="python">import seaborn as sns # Convert dataframes to tidy format dfall.set_index(["Name", "index", "variable"], inplace=1) dfall["vcs"] = dfall.groupby(level=["Name", "index"]).cumsum() dfall.reset_index(inplace=True) # Create color palette c = ["blue", "purple", "red", "green", "pink"] # Iterate through groups and plot stacked bars for i, g in enumerate(dfall.groupby("variable")): ax = sns.barplot(data=g[1], x="index", y="vcs",</code>
以上是如何在 Python 中为多个 DataFrame 创建集群堆积条形图?的详细内容。更多信息请关注PHP中文网其他相关文章!