1

I create a bar plot like this: enter image description here

But since each x axis label is one day of january (for example 1, 3, 4, 5, 7, 8, etc) I think the best way of showing this is something like

__________________________________________________  x axis
Jan   1   3   4   5   7   8  ...
2019

But I dont know how to do this with Pandas.

Here is my code:

import pandas as pd
import matplotlib.plt as plt    

df = pd.read_excel('solved.xlsx', sheetname="Jan")

fig, ax = plt.subplots()

df_plot=df.plot(x='Date', y=['Solved', 'POT'], ax=ax, kind='bar', 
        width=0.9, color=('#ffc114','#0098c9'), label=('Solved','POT'))


def line_format(label):
"""
Convert time label to the format of pandas line plot
"""
month = label.month_name()[:3]
if month == 'Jan':
    month += f'\n{label.year}'
return month

ax.set_xticklabels(map(lambda x: line_format(x), df.Date))   

The function was a solution provided here: Pandas bar plot changes date format

I dont know how to modify it to get the axis I want

My data example solved.xlsx:

Date       Q    A    B  Solved POT
2019-01-04 Q4   11   9    14    5 
2019-01-05 Q4   9    11   14    5  
2019-01-08 Q4   11   18   10    6 
2019-01-09 Q4   18   19   18    5
Laura
  • 1,192
  • 2
  • 18
  • 36
  • Can you provide sample data? – Scott Boston Jan 30 '19 at 16:50
  • I have edited my question – Laura Jan 30 '19 at 16:55
  • I think you are looking for something like [this](https://stackoverflow.com/questions/53490318/pandas-auto-datetime-format-in-matplotlib/53491017#53491017) or [this](https://stackoverflow.com/questions/49452407/matplotlib-time-axis-with-continuous-hours/49452680#49452680). Now the exact solution will not work with a pandas bar plot (which is categorical), but it may convey the idea. – ImportanceOfBeingErnest Jan 30 '19 at 17:11
  • Hi @ImportanceOfBeingErnest, I have tried the two of them without success – Laura Jan 30 '19 at 17:33
  • Yes, they were meant for you as a starting point to understand the problem and find a solution yourself. – ImportanceOfBeingErnest Jan 30 '19 at 17:36

1 Answers1

1

I have found a solution:

import pandas as pd
import matplotlib.plt as plt    

df = pd.read_excel('solved.xlsx', sheetname="Jan")

fig, ax = plt.subplots()

df_plot=df.plot(x='Date', y=['Solved', 'POT'], ax=ax, kind='bar', 
        width=0.9, color=('#ffc114','#0098c9'), label=('Solved','POT'))


def line_format(label):
"""
Convert time label to the format of pandas line plot
"""
day = label.day
if day == 2:
    day = str(day) + f'\n{label.month_name()[:3]}'
return day

ax.set_xticklabels(map(lambda x: line_format(x), df.Date)) 

plt.show()

In my particular case I didnt have the date 2019-01-01 . So the first day for me was Jan 2

enter image description here

Laura
  • 1,192
  • 2
  • 18
  • 36