datetime x轴matplotlib标签导致不受控制的重叠 [英] datetime x-axis matplotlib labels causing uncontrolled overlap

查看:186
本文介绍了datetime x轴matplotlib标签导致不受控制的重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用'pandas.tseries.index.DatetimeIndex'绘制熊猫series. x轴标签顽固地重叠,即使提出了几种建议的解决方案,我也无法使它们表现出来.

I'm trying to plot a pandas series with a 'pandas.tseries.index.DatetimeIndex'. The x-axis label stubbornly overlap, and I cannot make them presentable, even with several suggested solutions.

我尝试了 stackoverflow解决方案,建议使用autofmt_xdate ,但这无济于事.

I tried stackoverflow solution suggesting to use autofmt_xdate but it doesn't help.

我也尝试了对plt.tight_layout()的建议,但没有效果.

I also tried the suggestion to plt.tight_layout(), which fails to make an effect.

ax = test_df[(test_df.index.year ==2017) ]['error'].plot(kind="bar")
ax.figure.autofmt_xdate()
#plt.tight_layout()
print(type(test_df[(test_df.index.year ==2017) ]['error'].index))

更新:我正在使用条形图是一个问题.定期的时间序列图显示了管理良好的标签.

UPDATE: That I'm using a bar chart is an issue. A regular time-series plot shows nicely-managed labels.

推荐答案

pandas条形图是分类图.它在刻度的整数位置为每个索引显示一个条形图.因此,第一个条形图在位置0,第二个条形图在位置1,依此类推.标签对应于数据框的索引.如果您有100条,则最终将有100个标签.这是有道理的,因为大熊猫无法知道是否应将它们视为类别或序数/数字数据.

A pandas bar plot is a categorical plot. It shows one bar for each index at integer positions on the scale. Hence the first bar is at position 0, the next at 1 etc. The labels correspond to the dataframes' index. If you have 100 bars, you'll end up with 100 labels. This makes sense because pandas cannot know if those should be treated as categories or ordinal/numeric data.

如果改为使用普通的matplotlib条形图,它将对数据框索引进行数字处理.这意味着条形图根据实际日期进行定位,标签根据自动行情自动收录器放置.

If instead you use a normal matplotlib bar plot, it will treat the dataframe index numerically. This means the bars have their position according to the actual dates and labels are placed according to the automatic ticker.

import pandas as pd
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt

datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=42).tolist()
df = pd.DataFrame(np.cumsum(np.random.randn(42)), 
                  columns=['error'], index=pd.to_datetime(datelist))

plt.bar(df.index, df["error"].values)
plt.gcf().autofmt_xdate()
plt.show()

这样的好处是,另外,可以使用matplotlib.dates定位符和格式化程序.例如.用自定义格式标记每个月的第一天和第十五天

The advantage is then in addition that matplotlib.dates locators and formatters can be used. E.g. to label each first and fifteenth of a month with a custom format,

import pandas as pd
import numpy as np; np.random.seed(42)
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

datelist = pd.date_range(pd.datetime(2017, 1, 1).strftime('%Y-%m-%d'), periods=93).tolist()
df = pd.DataFrame(np.cumsum(np.random.randn(93)), 
                  columns=['error'], index=pd.to_datetime(datelist))

plt.bar(df.index, df["error"].values)
plt.gca().xaxis.set_major_locator(mdates.DayLocator((1,15)))
plt.gca().xaxis.set_major_formatter(mdates.DateFormatter("%d %b %Y"))
plt.gcf().autofmt_xdate()
plt.show()

这篇关于datetime x轴matplotlib标签导致不受控制的重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆