pandas 熊猫绘图 - x 轴被转换为浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48715330/
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 05:09:15  来源:igfitidea点击:

pandas plotting - x axis gets transformed to floats

pythonpandasmatplotlib

提问by jxn

I am trying to plot my data grouped by year, and for each year, i want to count the number of users. Below, i just transformed the date column from float to integer.

我试图绘制按年份分组的数据,并且每年,我想计算用户数量。下面,我只是将日期列从浮点数转换为整数。

This is my plotenter image description here

这是我的阴谋在此处输入图片说明

If you see the x-axis, my year ticker seems to have become a float and the each ticker is 0.5 tick apart.

如果你看到 x 轴,我的年份代码似乎变成了一个浮点数,并且每个代码间隔 0.5 个刻度。

How do i make this purely an integer?

我如何使这纯粹是一个整数?



Changing the groupby has the same result: enter image description here

更改 groupby 具有相同的结果: 在此处输入图片说明



ticks are still 2 spaces apart after converting the year column to a string format

将年份列转换为字符串格式后,刻度仍然相隔 2 个空格

df['year'] = df['year'].astype(str)

: enter image description here

在此处输入图片说明

回答by ImportanceOfBeingErnest

The expectation that using integer data will lead a matplotlib axis to only show integers is not justified. At the end, each axis is a numeric float axis.

使用整数数据会导致 matplotlib 轴仅显示整数的期望是不合理的。最后,每个轴都是一个数字浮点轴。

The ticks and labels are determined by locators and formatters. And matplotlib does not know that you want to plot only integers.

刻度和标签由定位器和格式器确定。并且 matplotlib 不知道您只想绘制整数。

Some possible solutions:

一些可能的解决方案:

Tell the default locator to use integers

告诉默认定位器使用整数

The default locator is a AutoLocator, which accepts an attribute integer. So you may set this attribute to True:

默认定位器是 a AutoLocator,它接受一个属性integer。因此,您可以将此属性设置为True

ax.locator_params(integer=True)

Example:

例子:

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

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

ax = data.plot(x="year",y="count")
ax.locator_params(integer=True)

plt.show()

Using a fixed locator

使用固定定位器

You may just tick only the years present in the dataframe by using ax.set_ticks().

您可以使用 仅勾选数据框中存在的年份ax.set_ticks()

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

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

data.plot(x="year",y="count")
plt.gca().set_xticks(data["year"].unique())
plt.show()

Convert year to date

将年份转换为日期

You may convert the year column to a date. For dates much nicer ticklabeling takes place automatically.

您可以将年份列转换为日期。对于日期,自动进行更好的刻度标记。

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

data = pd.DataFrame({"year" : [2010,2011,2012,2013,2014],
                     "count" :[1000,2200,3890,5600,8000] })

data["year"] = pd.to_datetime(data["year"].astype(str), format="%Y")
ax = data.plot(x="year",y="count")

plt.show()

In all cases you would get something like this:

在所有情况下,您都会得到如下信息:

enter image description here

在此处输入图片说明

回答by AmourK

import matplotlib.pyplot as plt

# Use min and max to get the range of years to use in axis ticks
year_min = df['year'].min()
year_max = df['year'].max()

df['year'] = df['year'].astype(str) # Prevents conversion to float

plt.xticks(range(year_min, year_max, 1)) # Sets plot ticks to years within range

Hope this helps!

希望这可以帮助!