首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Twinx使标签消失

Twinx使标签消失
EN

Stack Overflow用户
提问于 2022-08-19 09:18:18
回答 1查看 71关注 0票数 1

在使用twinx函数时,设置x标签有问题。我的原始数据是熊猫的数据,即df,它有三个属性,“name”=产品名,“sold”=出售物品的数量,以及“收益”。它的名字是熊猫系列(比如"2洗发水“),但我不能把它设置为x滴答标签(见下图)。如何设置x标签以显示产品名称?

代码语言:javascript
复制
fig = plt.figure()  # Create matplotlib figure

    ax = fig.add_subplot(111)  # Create matplotlib axes
    ax2 = ax.twinx()  # Create another axes that shares the same x-axis as ax.

    width = 0.4

    df.sold.plot(kind='bar', color='red', ax=ax, width=width, position=1, rot=90)
    df.revenue.plot(kind='bar', color='blue', ax=ax2, width=width, position=0, rot=90)

    # print(type(df['name']), "\n", df['name'])

    ax.set_ylabel('Sold')
    ax2.set_ylabel('Revenue')
    ax.legend(['Sold'], loc='upper left')
    ax2.legend(['Revenue'], loc='upper right')
    plt.show()

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-08-19 10:15:56

您需要使用set_xticklabels()来设置X轴的标签以显示字段。在绘制图形后添加这一行。

代码语言:javascript
复制
ax.set_xticklabels(df.Name)

你会得到下面的情节。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73414522

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档