2

I want to draw a vertical line for a specific month date (period).

The expected graph is : enter image description here

The dataframe I want to draw (named complete) look like that :

      ENTRETIEN FRAIS_FINANCIER
month       
2017-10 57.48   311.34
2017-11 0.00    0.00
2017-12 0.00    0.00
2018-01 0.00    0.00
2018-02 57.48   311.34
2018-03 57.48   311.34
2018-04 0.00    368.82
2018-05 0.00    368.82
2018-06 0.00    368.82
2018-07 0.00    368.82
2018-08 0.00    368.82
2018-09 0.00    368.82
2018-10 0.00    0.00
2018-11 0.00    368.82
2018-12 0.00    368.82
2019-01 57.48   311.34
2019-02 57.48   311.34
2019-03 57.48   311.34
2019-04 57.48   311.34
2019-05 0.00    368.82
2019-06 0.00    368.82
2019-07 0.00    368.82
2019-08 0.00    368.82
2019-09 0.00    368.82
2019-10 0.00    0.00
2019-11 0.00    368.82

Here is my code :

ax = complete.plot(kind='bar', stacked=True)
# Limite
plt.axvline(max(complete.index), color="red", linestyle="--", lw=2, label="Limite", ax=ax)

But with the code I have this error :

TypeError: '>' not supported between instances of 'float' and 'Period'

I can't figure out how to do it properly, can you help me ?

LCMa
  • 445
  • 3
  • 13
  • `plt.axvline(x=complete.index.size, ymin=0, ymax=complete.sum(axis=1).max(), color="red", linestyle="--", lw=2, label="Limite", ax=ax)` – JE_Muc Oct 16 '20 at 09:56
  • Could you please provide sample data (data as an executable code snippet instead of an image)? – JE_Muc Oct 16 '20 at 09:58
  • 1
    @Scotty1- Sorry, I edited my message – LCMa Oct 16 '20 at 10:02
  • If I put your line, I get : 'Line2D' object has no property 'ax'. I tried removing all ax and I get the graph, with the legend having limit, but the line is not visible on graph ... – LCMa Oct 16 '20 at 10:04
  • What you want to achieve is to draw a line horizontally, isn't that what you want to achieve? You can draw it with this. `ax.axhline(max(complete.index), color="red", linestyle="--", lw=2, label="Limite")` – r-beginners Oct 16 '20 at 10:05
  • @LCMa Thanks for the edit and no problem at all. :) Yep, forgot to remove the `ax` argument from your example. – JE_Muc Oct 16 '20 at 10:11

2 Answers2

3

Plot the vertical line with:

ax = complete.plot(kind='bar', stacked=True)
plt.axvline(
    x=complete.index.size - 1, ymin=0, ymax=complete.sum(axis=1).max(), 
    color="red", linestyle="--", lw=2, label="Limite"
)

Since barplot transforms the x-axis limits to numeric values increasing by 1 for each bar, the last bar has the "index" complete.index.size - 1. Thus this is the x-position for your vertical line.

JE_Muc
  • 5,403
  • 2
  • 26
  • 41
  • 1
    Thanks a lot ! What I was missing was this barplot x-axis transformation – LCMa Oct 16 '20 at 10:11
  • You are welcome! Yep, when plotting with pandas, this is done automatically, thus it is easy to oversee it. – JE_Muc Oct 16 '20 at 10:12
0

try this:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np

date_rng = pd.date_range('2017-10-01','2019-11-01', freq='1M')
val = np.random.randint(0,50,(25,))
val2 = np.random.randint(0,350,(25,))
complete = pd.DataFrame({'month':pd.to_datetime(date_rng, format='%Y-%m'), 'ENTRETIEN':val,'FRAIS_FINANCIER':val2})
ax = complete.plot(x='month', kind='bar', stacked=True)
ax.axvline(max(complete.index), color="blue", linestyle="--", lw=2, label="Limite")

enter image description here

r-beginners
  • 31,170
  • 3
  • 14
  • 32