【问题标题】:plotting SNS line chart with category x axis使用类别 x 轴绘制 SNS 折线图
【发布时间】:2021-08-14 20:29:09
【问题描述】:

我正在使用它对值进行分组:

代码:

df['new_carat'] = pd.cut(x=df['carat'], bins=[0, 0.49, 0.69, 0.99, 1.99, 3], 
                         labels=['<0.5', '0.5-0.69', '0.7-0.99', '1-1.99', '>2'])

但是当我尝试绘制折线图时,它给了我问题。

代码:

ax1 = sns.countplot(x='new_carat', data = df, palette='husl')
ax1.set_xlabel('Carat')
ax1.set_ylabel('Count')
ax1.set_title('Carat Count & Avg Price')
ax2 = ax1.twinx()
ax2 = sns.lineplot("new_carat",'price', data = df, color = red)
ax2.set_ylabel('Average Price')

plt.show

错误消息说“ConversionError:无法将值转换为轴单位”

【问题讨论】:

    标签: python matplotlib seaborn line-plot


    【解决方案1】:

    将线图改为点图,解决了我的问题。

    ax1 = sns.countplot(x='new_carat', data = df, palette='husl')
    ax1.set_xlabel('Carat')
    ax1.set_ylabel('Count')
    ax1.set_title('Carat Count & Avg Price')
    ax2 = ax1.twinx()
    ax2 = sns.pointplot(x="new_carat", y='price', data = df, color = 'red')
    ax2.set_ylabel('Average Price')
    
    plt.show
    

    【讨论】:

      猜你喜欢
      • 2023-03-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多