如何将多个海洋计数图与共享的y轴组合

时间:2020-10-01 10:19:47

标签: python-3.x matplotlib seaborn

嗨,我正在尝试合并多个海军陆战队。 因此,我正在使用以下代码制作第一号地块(如下所示):

ax1=sns.countplot(x="CommsMailSecondary", data=df, palette="Greens_d",
              order=df.CommsMailSecondary.value_counts().iloc[:5].index)
ax1.set_xticklabels(ax.get_xticklabels(), rotation=90, ha="right")
plt.tight_layout()
plt.show()

enter image description here

我正在使用以下代码制作第二个地块(如下所示):

import React, { useState, Fragment } from "react";

export default function App() {
  const [showInputs, setInputs] = useState([]);

  const handleClick = () => {
    setInputs((prev) => {
      const i = prev.length + 1;
      return [
        ...prev,
        <Fragment key={i}>
          <input type="text" />
          <br />
        </Fragment>
      ];
    });
  };

  return (
    <div className="App">
      <button onClick={handleClick}>Click me</button>
      <br />
      {showInputs}
    </div>
  );
}

enter image description here

我的问题是-如何结合这两个(或更多)计数图,以使“ True / False”成为具有共同y轴的图例(如下所示)?我在互联网上搜索了很多东西,但似乎找不到找到结合这种特殊类型的海洋计数图的方法,谢谢。

期望的情节:

enter image description here

1 个答案:

答案 0 :(得分:1)

您只能在熊猫中轻松做到这一点:

df_new = df.apply(pd.value_counts).T
ax=df_new.plot.bar()
ax.set_xticklabels(ax.get_xticklabels(), rotation=90, ha="right")
plt.tight_layout()
plt.show();

enter image description here