问题
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.
What I currently have:
Current Data Frame
The plot using : df.plot(x=None, y=['Published NIV','Future NIV'], kind='line')
Circled in blue is how I want the axis to look
回答1:
This is the question I've worked most on Stackoverflow. I hope this fits your problem perfectly.
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))
And the output is:
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.
来源:https://stackoverflow.com/questions/54055448/how-do-i-get-a-multilevel-x-axis-labelled-plot-in-pandas