【问题标题】:Seaborn twin plot not working as expectedSeaborn 双胞胎情节未按预期工作
【发布时间】:2020-10-07 18:50:27
【问题描述】:

我想使用 seaborn 制作双胞胎图,但在某些情况下,条形图最终会出现在图的左侧,而对于大多数其他变量来说这很好。应该修复什么? plot

plt.figure(figsize=(15,5))

ax1 = sns.barplot(x=cat, y="Number of opportunities", data=df)
ax1.set_ylabel("Number of opportunities", fontsize=16)
plt.xticks(
    rotation=45, 
    horizontalalignment='right',
    fontweight='light',
    fontsize='x-large'  
)

color = 'c'

ax2 = ax1.twinx()
ax2 = sns.lineplot(x=cat, y="Win Ratio", data=df, linewidth=2, color=color)

【问题讨论】:

  • Seaborn 对条形图使用分类 x 轴。以及用于线图的数字 x 轴(当数据为数字时)。为了让两者一起工作,您可以将数据框的“cat”列转换为字符串。 df['cat'] = df['cat'].astype(str).

标签: plot seaborn bar-chart linechart


【解决方案1】:

正如 Johan 所解释的,'cat' 列应转换为字符串:

df['cat'] = df['cat'].astype(str). – JohanC 

【讨论】:

    猜你喜欢
    • 2021-07-27
    • 2012-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多