Pandas と Matplotlib または Seaborn を使用してクラスター積み上げ棒グラフを作成するにはどうすればよいですか?
クラスター積み上げ棒グラフの作成
問題:
インデックスは同じだが列が異なる可能性がある 2 つのデータフレーム df1 と df2 について考えます。行はカテゴリを表し、各列は指標を表します。目標は、各カテゴリの棒がグループ化され、各データフレームの棒が互いの上に積み上げられるクラスター積み上げ棒グラフを作成することです。
Pandas と Matplotlib を使用した解決策:
<code class="python">import pandas as pd import matplotlib.pyplot as plt import matplotlib.cm as cm def plot_clustered_stacked(df_list, labels=None, title="Clustered Stacked Bar Plot"): n_dataframes = len(df_list) n_columns = len(df_list[0].columns) n_index = len(df_list[0].index) fig, ax = plt.subplots() # Iterate through each dataframe for i, df in enumerate(df_list): # Plot the bars for the current dataframe df.plot(kind="bar", ax=ax, linewidth=0, stacked=True, legend=False, grid=False) # Adjust the position and width of the bars for df, j in zip(df_list, range(n_dataframes)): for n, rect in enumerate(ax.patches): if rect.get_y() == 0: # Stacked bar for dataframe df rect.set_x(rect.get_x() + j / float(n_dataframes)) rect.set_width(1 / float(n_dataframes)) # Set the x-axis labels and ticks ax.set_xticks(np.arange(0, n_index) + 0.5) ax.set_xticklabels(df.index) # Add a legend for the dataframes plt.legend([df.stack(level=0).index[0] for df in df_list], labels) # Set the plot title ax.set_title(title) # Create example dataframes df1 = pd.DataFrame(np.random.rand(4, 3), index=["A", "B", "C", "D"], columns=["x", "y", "z"]) df2 = pd.DataFrame(np.random.rand(4, 3), index=["A", "B", "C", "D"], columns=["x", "y", "z"]) # Plot the clustered stacked bar plot plot_clustered_stacked([df1, df2], labels=["df1", "df2"])</code>
ログイン後にコピー
Seaborn と Pandas を使用したソリューション:
<code class="python">import seaborn as sns # Concatenate the dataframes into a single dataframe with a wide format df = pd.concat([df1.reset_index().melt(id_vars=["index"]), df2.reset_index().melt(id_vars=["index"])]) # Plot the clustered stacked bar plot g = sns.FacetGrid(data=df, col="variable", hue="index") g.map_dataframe(sns.barplot, order=df["index"].unique())</code>
ログイン後にコピー
以上がPandas と Matplotlib または Seaborn を使用してクラスター積み上げ棒グラフを作成するにはどうすればよいですか?の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事
R.E.P.O.説明されたエネルギー結晶と彼らが何をするか(黄色のクリスタル)
2週間前
By 尊渡假赌尊渡假赌尊渡假赌
レポ:チームメイトを復活させる方法
4週間前
By 尊渡假赌尊渡假赌尊渡假赌
ハローキティアイランドアドベンチャー:巨大な種を手に入れる方法
3週間前
By 尊渡假赌尊渡假赌尊渡假赌
スプリットフィクションを打ち負かすのにどれくらい時間がかかりますか?
3週間前
By DDD
R.E.P.O.ファイルの保存場所:それはどこにあり、それを保護する方法は?
3週間前
By DDD

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック
Gmailメールのログイン入り口はどこですか?
7315
9


Java チュートリアル
1625
14


CakePHP チュートリアル
1348
46


Laravel チュートリアル
1260
25


PHP チュートリアル
1207
29

