如何在 pandas 中获得标有"plot"的多级x轴? [英] How do I get a multilevel x axis labelled plot in pandas?

查看:116
本文介绍了如何在 pandas 中获得标有"plot"的多级x轴?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个多索引的熊猫数据框,我可以根据需要生成正确形状的图,但是x轴仅显示为我的多索引的列标题.我正在寻找一种获得一组分层标签的方法.

I Have a multi-indexed pandas data frame, I can produce the correctly shaped plot for what I require however the x axis is displayed only as the column headers of my multi index. I am after a way of getting a set of layered labels.

我目前拥有的是:

当前数据框

使用df.plot(x = None,y = ['Published NIV' ,'Future NIV'],kind ='line')

用蓝色圆圈圈出的是我希望轴的外观

推荐答案

这是我在Stackoverflow上工作最多的问题.我希望这完全适合您的问题.

This is the question I've worked most on Stackoverflow. I hope this fits your problem perfectly.

因此,假设您的期间值是1到48,那是我可以获得此方法的唯一方法,这是半小时的数据,我所做的是我在提供日期的半小时内更改了这些值,然后使用格式将其更改为datetimeindex,然后创建绘图.

So, your period values are from 1 to 48, assuming and that was the only way I could get this, that this is half hourly data, what I did was that I changed these values in half hours of the date provided and then using formatting I changed it to datetimeindex and then created the plot.

所以这是代码和最终输出:

So here's the code and final output:

m=pd.read_csv("Data3.csv")
final=m[['Published NIV','Future NIV']]

new2=['0:00:00' if a==1 else str(a//2-1)+':30:00' if a%2==0 else str(a//2)+':00:00' for a in m['Settlement Period']]

alpha=m['Settlement Date']+'-'+new2

final.index=pd.to_datetime(alpha,format='%Y-%m-%d-%X')
final.plot(figsize=(15,9))

输出为:

此代码的唯一缺点是您不会获得1至48的期间号,通过一些变通办法,我可以尝试带回,但请告诉我这是否适合您的情况.

The only downside I see of this code is that you won't get those period numbers 1 to 48, which with some workaround I can try to bring back, but tell me if this suits your case.

这篇关于如何在 pandas 中获得标有"plot"的多级x轴?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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