【问题标题】:How do I add multiple markers to a stripplot in seaborn?如何在 seaborn 的条形图中添加多个标记?
【发布时间】:2016-12-03 16:52:52
【问题描述】:

我想知道如何在同一个带状图中获得多个标记。

tips = sns.load_dataset("tips")

coldict={'Sun':'red','Thur':'blue','Sat':'yellow','Fri':'green'}
markdict={'Sun':'x','Thur':'o','Sat':'o','Fri':'o'}

tips['color']=tips.day.apply(lambda x: coldict[x])
tips['marker']=tips.day.apply(lambda x: markdict[x])

m=sns.stripplot('size','total_bill',hue='color',\
                marker='marker',data=tips, jitter=0.1, palette="Set1",\
                split=True,linewidth=2,edgecolor="gray")

这似乎不起作用,因为标记只接受单个值。

另外,我最好将相应的“太阳”值设为透明的红色三角形。知道如何实现吗?

谢谢。

编辑: 所以一个更好的方法是声明一个 my_ax = plt.axes() 并将 my_ax 传递给每个 stripplot(ax=my_ax)。我相信这是应该做的。

【问题讨论】:

    标签: python pandas matplotlib seaborn


    【解决方案1】:

    注意这有点hacky,但你去吧:

    import sns
    
    tips = sns.load_dataset("tips")
    
    plt.clf()
    thu_fri_sat = tips[(tips['day']=='Thur') | (tips['day']=='Fri') | (tips['day']=='Sat')]
    colors = ['blue','yellow','green','red']
    m = sns.stripplot('size','total_bill',hue='day',
                      marker='o',data=thu_fri_sat, jitter=0.1, 
                      palette=sns.xkcd_palette(colors),
                      split=True,linewidth=2,edgecolor="gray")
    
    sun = tips[tips['day']=='Sun']
    n = sns.stripplot('size','total_bill',color='red',hue='day',alpha='0.5',
                      marker='^',data=sun, jitter=0.1, 
                      split=True,linewidth=0)
    handles, labels = n.get_legend_handles_labels()
    n.legend(handles[:4], labels[:4])
    plt.savefig('/path/to/yourfile.png')
    

    【讨论】:

    • 谢谢。这很好用!如果我在此之上添加分面网格,事情会变得复杂吗?
    • 嘿没问题。如果您不介意用该代码提出另一个问题,我们会看看我们能做什么。
    • 嗨。我刚刚发现这可能不正确?如果我理解正确,我们正在尝试使用标记“o”绘制周四、周五、坐,并使用标记“^”绘制太阳。但是,图中似乎缺少一些数据。要看到这一点,请禁用 jitter & split,只绘制 thu、fri、sat,你会看到 4 size=1 points。或者,只需打印thu_fri_sat[thu_fri_sat["size"] == 1]。有 4 行,但图中未显示。
    【解决方案2】:

    Lmplot 是你的朋友!但是,无法添加倍数:/ [或者至少我还没有弄清楚]

    enter image description here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-12
      • 2015-11-22
      • 1970-01-01
      • 2020-07-29
      • 2018-09-24
      • 1970-01-01
      相关资源
      最近更新 更多