首页 > 解决方案 > Having trouble with x axis date in line chart (pandas)

问题描述

This seems very simple and I am not sure why it's not working. It works fine when I use a stacked bar chart, but when I use a line chart the formatting is not correct (or something else is not working?). For example, the stacked barchart looks like enter image description here

Very simple logic to create the above chart:

pivot_df = base_df.pivot(index='date', columns='column10', values='values')
pivot_df1 = pivot_df.fillna(0)
pivot_df1 = pivot_df1.astype(float)
pivot_df1 = pivot_df1.plot.bar(stacked=True, figsize=(10,7))

Now I am trying to create a line chart using the same base dataset (so same dates) but slightly different columns/values. I filter the dataset on one condition and remove a few columns (not sure if this is causing the issue?).

base_df1 = base_df[((base_df['filter_column'] == 'this'))]
base_df1 = base_df1[['date', 'column2', 'column3', 'column4']]
base_df1 = base_df1.fillna(0)
base_df1 = base_df1.astype(float) 
base_df1_piv = base_df1.pivot_table(index='date',columns='column2', values = 'column3')
base_df1_piv.plot.line()

x axis date issue

I feel like I am missing something very obvious? Any help is appreciated.

标签: pythonpandasmatplotlib

解决方案


推荐阅读