LeoGER
LeoGER

Reputation: 357

line chart with months for x-labels but using weekly data

Below is script for a simplified version of the df in question:

import pandas as pd
    
df = pd.DataFrame({ 
                   'week': [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17],
                   'month' : ['JAN','JAN ','JAN','JAN','FEB','FEB','FEB','FEB','MAR','MAR',
                              'MAR','MAR','APR','APR','APR','APR','APR'],
                   'weekly_stock' : [4,2,5,6,2,3,6,8,7,9,5,3,5,4,5,8,9]
                 })
df

    week    month   weekly_stock
0   1       JAN           4
1   2       JAN           2
2   3       JAN           5
3   4       JAN           6
4   5       FEB           2
5   6       FEB           3
6   7       FEB           6
7   8       FEB           8
8   9       MAR           7
9   10      MAR           9
10  11      MAR           5
11  12      MAR           3
12  13      APR           5
13  14      APR           4
14  15      APR           5
15  16      APR           8
16  17      APR           9

As it currently stands, the script below produces a bar chart with week for x-labels

# plot chart
labels=df.week

line=df['weekly_stock']

fig, ax = plt.subplots(figsize=(20,8))

line1=plt.plot(line, label = '2019')

ax.set_xticks(x)
ax.set_xticklabels(labels, rotation=0)

ax.set_ylabel('Stock')
ax.set_xlabel('week')
plt.title('weekly stock')

enter image description here

However, I would like to have the month as the x-label.

INTENDED PLOT:

enter image description here

Any help would be greatly appreciated.

Upvotes: 0

Views: 990

Answers (1)

Ukrainian-serge
Ukrainian-serge

Reputation: 854

My recommendation is to have a valid datetime values column instead of 'month' and 'week', like you have. Matplotlib is pretty smart when working with valid datetime values, so I'd structure the dates like so first:

import pandas as pd
import matplotlib.pyplot as plt

# valid datetime values in a range
dates = pd.date_range(
    start='2019-01-01', 
    end='2019-04-30',
    freq='W',         # weekly increments
    name='dates', 
    closed='left'
    )             

weekly_stocks = [4,2,5,6,2,3,6,8,7,9,5,3,5,4,5,8,9]

df = pd.DataFrame(
    {'weekly_stocks': weekly_stocks},
    index=dates # set dates column as index                   
    )

df.plot(
    figsize=(20,8),
    kind='line', 
    title='Weekly Stocks',
    legend=False,
    xlabel='Week',
    ylabel='Stock'
    )

plt.grid(which='both', linestyle='--', linewidth=0.5)

enter image description here

Now this is a fairly simple solution. Take notice that the ticks appear exactly where the weeks are; Matplotlib did all the work for us!

  1. (easier) You can either lay the "data foundation" prior to plotting correctly, i.e., format the data for Matplotlib to do all the work like we did above(think of the ticks being the actual date-points created in the pd.date_range()).

  2. (harder) Use tick locators/formatters as mentioned in docs here

Hope this was helpful.

Upvotes: 2

Related Questions