【问题标题】:seaborn plot with two y axis具有两个 y 轴的 seaborn 图
【发布时间】:2021-08-22 12:28:52
【问题描述】:

我有一些数据,需要生成像这张图片一样的图,我只是想知道如何使用 python seaborn 散点图来做到这一点? 谢谢,堆! 示例:

【问题讨论】:

  • 这里有与您和answers相同的问题。
  • 使用ax2 = ax1.twinx(),然后在ax2上绘制第二个数据集

标签: python seaborn scatter


【解决方案1】:

这是一个使用seaborn.scatterplot 的最小示例:

import pandas as pd
import seaborn as sns
import numpy as np
np.random.seed(0)
df1 = pd.DataFrame({'x': np.random.random(size=10),
                    'y1': np.random.random(size=10),
                   })
df2 = pd.DataFrame({'x': np.random.random(size=10),
                    'y2': np.random.random(size=10)*100,
                   })
ax1 = plt.subplot()
ax2 = ax1.twinx()
sns.scatterplot(data=df1, x='x', y='y1', ax=ax1)
sns.scatterplot(data=df2, x='x', y='y2', color='r', ax=ax2)
ax2.tick_params(axis='y', colors='red')

输出:

【讨论】:

    【解决方案2】:

    试试这个:(设置color使用hue,设置shape使用style

    import seaborn as sns
    import pandas as pd
    import numpy as np
    import random
    
    sns.set_theme(style="white")
    
    data = pd.DataFrame({'x': np.random.random(size=300),
                        'y': np.random.random(size=300),
                         'color' : random.choices([0,1,2], k=300),
                         'weight' : random.choices([10,20], k=300)
                       })
    
    
    
    sns.relplot(x="x", y="y", hue="color", size="weight", style = "color",
                sizes=(40, 400), alpha=.5, palette="muted", data=data)
    

    输出:

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2023-04-07
      • 2020-08-29
      • 2023-03-10
      • 1970-01-01
      • 2021-09-24
      • 2021-11-07
      • 2020-07-19
      • 1970-01-01
      相关资源
      最近更新 更多