【问题标题】:Index labels are not displaying - Pandas(Series)索引标签不显示 - Pandas(系列)
【发布时间】:2018-10-04 02:43:59
【问题描述】:

我正在使用 pandas 和 matplotlib,我正在尝试通过 熊猫系列中的索引

在 x 轴上设置标签
import pandas as pd
import matplotlib.pyplot as plt

index = ['apples','oranges','cherries','bananas']
quantity = [20,30,40,50]

s = pd.Series(quantity, index = index)
s.plot()
plt.title("pandas series")
plt.show()

它在 x 轴上显示没有标签的输出, 我需要水果名称作为 X 轴上的标签。 谁能帮我解决这个错误?

提前致谢!

【问题讨论】:

  • 由于您的 x 值没有内在顺序,您可能不应该使用线图。如果您执行s.plot(kind='bar'),那么标签将正确显示。

标签: pandas matplotlib


【解决方案1】:

Make pandas plot() show xlabel and xvalues 也可以看出,熊猫似乎存在一些问题(目前?)。

这里直接使用 matplotlib 也是一个不错的选择。只需将 s.plot() 替换为

plt.plot(s)

【讨论】:

    【解决方案2】:

    您只需要定义位置。这样做:

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    index = ['apples','oranges','cherries','bananas']
    quantity = [20,30,40,50]
    
    s = pd.Series(quantity, index = index)
    s.plot()
    plt.title("pandas series")
    plt.xticks(np.arange(4), index) 
    plt.show()
    

    【讨论】:

      【解决方案3】:

      在这里使用线图并没有什么意义,因为类别是独立的。

      试试条形图,它会自动包含标签。 s.plot(kind='bar')

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-04-24
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-22
        • 1970-01-01
        相关资源
        最近更新 更多