【问题标题】:Is there a way to plot this using seaborn?有没有办法使用seaborn来绘制这个?
【发布时间】:2020-06-07 08:45:37
【问题描述】:

我正在尝试使用 seaborn 将其可视化,但无法找到方法。

我的数据框如下所示:

Type    Vmax    Km
W1H     1.1031  34.976545
W1R     1.9014  200.745433
W1X     1.0447  20.796225

到目前为止我所尝试的:

sns.barplot(x='Vmax',y='Type',hue='Km',data=df,orient='h')

1 我得到的结果:

2 我想要的东西(像这样):

【问题讨论】:

    标签: python matplotlib data-visualization seaborn


    【解决方案1】:

    我认为您需要来自pandasmelt 函数。这使您可以获取一个变量,该变量指示要作为seabornhue 参数传递的测量类型(Km 或Vmax)(您可以查看此paper 以阅读有关此类“整洁”数据的更多信息表示):

    df = pd.DataFrame({"Type": ["W1H", "W1R", "W1X"], "Vmax": [1.1031, 1.9014, 1.0447],
                      "Km": [34.976545, 200.745433, 20.796225]})
    
    molten = pd.melt(df, id_vars="Type")
    
    # >>> molten
    #   Type variable       value
    # 0  W1H     Vmax    1.103100
    # 1  W1R     Vmax    1.901400
    # 2  W1X     Vmax    1.044700
    # 3  W1H       Km   34.976545
    # 4  W1R       Km  200.745433
    # 5  W1X       Km   20.796225
    
    f, ax = plt.subplots()
    sbn.barplot(y="Type", x="value", hue="variable", orient="h", data=molten)
    

    输出:

    但是,这两个测量值的比例相当不同,因此您也可以考虑将它们绘制在两个子图上:

    f, ax = plt.subplots(1, 2)
    sbn.barplot(y="Type", x="value", orient="h",
                data=molten.loc[molten["variable"] == "Vmax", :], ax=ax[0], color="black")
    sbn.barplot(y="Type", x="value", orient="h",
                data=molten.loc[molten["variable"] == "Km", :], ax=ax[1], color="black")
    ax[0].set(xlabel="Vmax", ylabel="")
    ax[1].set(xlabel="Km", ylabel="")
    f.set_size_inches(10, 5)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2020-10-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      • 2021-06-08
      • 2014-01-13
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多