pandas 转换错误:无法将值转换为轴单位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/58814857/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:25:00  来源:igfitidea点击:

ConversionError: Failed to convert value(s) to axis units

pythonpandasnumpymatplotlibseaborn

提问by pyxies

I have a dataframe ddcontaining 84 rows and 3 columns.

我有一个dd包含 84 行和 3 列的数据框。

Now I want to plot an Area Plot with it, and use its index as xticks, so I do the following:

现在我想用它绘制一个区域图,并使用它的索引作为 xticks,所以我执行以下操作:

dd.plot(kind='area')
plt.show()

But I got this result: result

但我得到了这个结果: 结果

(P.S. I dont have enough reputation to post a picture, so I put this link here.)

(PS 我没有足够的声望来张贴图片,所以我把这个链接放在这里。)

It turns out that some xticks are hidden automatically: there should be 84 xticks, but only 9 of them are shown (seems to be hidden automatically).

结果发现有些xticks是自动隐藏的:应该有84个xticks,但是只显示了9个(好像是自动隐藏的)。

I found a similar question here, but when I tried the method mentioned in the link, I got an CnoversionError:

我在这里发现了一个类似的问题,但是当我尝试链接中提到的方法时,我得到了一个CnoversionError

ConversionError: Failed to convert value(s) to axis units: Index(['!', '"', '#', '$', '%', '&', ''', '(', ')', '*', '+', ',', '-', '.',
       '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '<',
       '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
       'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
       'Y', 'Z', '[', '\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f',
       'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't'],
      dtype='object')

I noticed that the difference between my question and the link above is that the index of my DataFrame has a dtype object(they are string), and I found that if I change the index to a list of int, the error does go away.

我注意到我的问题和上面的链接之间的区别在于我的 DataFrame 的索引有一个 dtype object(它们是字符串),我发现如果我将索引更改为 int 列表,错误就会消失。

The code to reproduce the Error:

重现错误的代码:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

dd=pd.DataFrame(np.random.rand(84,3),index=[chr(ascii) for ascii in range(33,33+84)])

dd.plot(kind='area',xticks=dd.index)

plt.show()

Thanks for your kindly reply in advance!

感谢您提前回复!

采纳答案by Diziet Asahi

dd=pd.DataFrame(np.random.rand(84,3),index=[chr(ascii) for ascii in range(33,33+84)])
dd.plot(kind='area')
plt.xticks(range(0,len(dd.index)), dd.index)
plt.show()

enter image description here

在此处输入图片说明

回答by BhavyaPrabha

We need to provide index positions of the labels in the xticks function, labels order should be as per the index positions. xticks function takes 3 arguments,

我们需要在 xticks 函数中提供标签的索引位置,标签顺序应该按照索引位置。xticks 函数需要 3 个参数,

  1. ticksshould be position of indexes of the labels
  2. labelsargument takes the list of label values
  3. rotationtakes how the label should be presented in the plot

    x = df['state'] y = df['sales'] tickvalues = range(0,len(x)) // or tickvalues = df.index plt.figure(figsize = (20,5)) plt.xticks(ticks = tickvalues ,labels = labellist, rotation = 'vertical') plt.plot(x,y)

  1. 刻度应该是标签索引的位置
  2. 标签参数采用标签值列表
  3. 旋转需要如何在图中显示标签

    x = df['state'] y = df['sales'] tickvalues = range(0,len(x)) // or tickvalues = df.index plt.figure(figsize = (20,5)) plt.xticks(ticks = tickvalues ,labels = labellist, rotation = 'vertical') plt.plot(x,y)