0

I am trying to plot dates (N=50) on a 5 year time series chart and I'm having trouble trying to figure out how to run through an iteration on a for loop. Below is an example of what I'm trying to plot the dates on.

Visual of what I'm plotting dates on

Currently, I am trying:

for date in dataframe_with_dates.DATE:
    plt.axvline(x = date, color = 'g')

plt.show()

and I'm receiving an error of: Failed to convert value(s) to axis units: 'DATE'

I'm not sure if this has something to do with the dtype being datetime, or if I need to try another approach, but any advice/guidance is greatly appreciated!

Thank you!

This is what I am trying to accomplish: Example image

EDIT: Code to produce the plot

def plot_df(df_1, x, y, title = '', xlabel = 'DATE', ylabel = 'VALUE', dpi = 100):
    plt.figure(figsize = (25,5), dpi = dpi)
    plt.plot(x, y, color = 'tab:red')
    plt.gca().set(title = title, xlabel = xlabel, ylabel = ylabel)
    plt.show()

plot_df(df_VIX, x = df_VIX.DATE, y = df_VIX.AVG_VALUE, title = 'Daily VIX since 1990')

`

data_test = [['2016-01-04', 22.48, 23.36, 20.67, 20.70, 21.8025],
         ['2016-01-05', 20.75, 21.06, 19.25, 19.34, 20.1],
         ['2016-01-06', 21.67, 21.86, 19.8, 20.59, 20.98],
         ['2016-01-07', 23.22, 25.86, 22.4, 24.99, 24.1175],
         ['2016-01-08', 22.96, 25.86, 22.40, 24.99, 24.89]]
df_test = pd.DataFrame(data_test, columns = ['DATE','OPEN','HIGH','LOW','CLOSE', 'AVG_VALUE'])

df_test['DATE'] = pd.to_datetime(df_test['DATE'])

This will reproduce a sample of the exact data I'm using.

birdman
  • 249
  • 1
  • 13
  • Can you provide more information on `dataframe_with_dates` (content, dtypes, and the lines of code that produce the current plot)? Indeed I can't reproduce your error: With a dataframe containing datetimes, I am able to display the vertical lines. Pandas version may be useful information too. – nonin Aug 10 '21 at 18:17
  • `dataframe_with_dates` includes 6 variables of `DATE, OPEN, HIGH, LOW, CLOSE, AVG`. All float64 except for `DATE`, which is datetime64[ns] The lines of code producing that plot are edited in the OP. – birdman Aug 10 '21 at 18:38
  • It'd help if you shared a code snippet with a few lines of your dataframe so we can copy it into our editors and prototype a solution. So far, without seeing the code and values, it's difficult to help. Thanks! – TC Arlen Aug 10 '21 at 19:32
  • 1
    @TCArlen I have added it to the OP. – birdman Aug 10 '21 at 19:56
  • @nonin I added it to OP – birdman Aug 10 '21 at 19:56
  • [Vertical Lines on TimeSeries plot](https://stackoverflow.com/a/64035939/7758804) – Trenton McKinney Aug 10 '21 at 20:40
  • With pandas 1.1.4 and matplotlib 3.4.2, everything works fine. – nonin Aug 11 '21 at 07:12

1 Answers1

0

I think this is what you want:

df_test.plot(x='DATE', y='OPEN')

Or replace y='OPEN' with another column to plot. The x-axis will be formatted automatically by pandas to be similar to what you showed in the figure.

TC Arlen
  • 1,442
  • 2
  • 11
  • 19