Python Forum
How to create a plot with line graphs and vertical bars
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to create a plot with line graphs and vertical bars
#1
Hi,

I am trying to figure out how to create a graph similar to the following:

[Image: LHt8r.png]

where the x axis has the monthly time data and the y axis represents the Dow Jones average (DJI column on my sample data). I am trying to get the recession months to plot as vertical bars corresponding to the "Regime" column in my sample data.

Here is the code; files are also attached – any help would be greatly appreciated:

import pandas as pd
import plotly.graph_objects as go
import matplotlib as mpl
%matplotlib inline
import matplotlib.pyplot as plt
from matplotlib.pyplot import figure
import datetime
from datetime import date
import numpy as np

#import data
history = pd.read_csv("sample_data.csv")
history = history.replace(np. nan,'',regex = True)

#filter out recession dates
recession = history.loc[history["Regime"] == "Recession", "Date"]

#plot axvspan for every recession month
for month in recession:
    plt.axvspan(recession, recession + datetime.timedelta(days=1), color="grey", alpha=0.5)   
plt.show()

Attached Files

.csv   sample_data.csv (Size: 1.69 KB / Downloads: 4)
.py   recession_graph.py (Size: 599 bytes / Downloads: 2)
Reply


Messages In This Thread
How to create a plot with line graphs and vertical bars - by devansing - Feb-22-2023, 08:14 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add multiple vertical rectangles to a chart in plotly? devansing 2 1,276 Jun-20-2023, 07:55 AM
Last Post: snippsat
  Plot Line chart based on the input parthi1705 4 3,524 Feb-28-2023, 12:08 PM
Last Post: get2sid
  Pick a line in a plot with matplotlib Romain 3 5,714 Feb-28-2023, 11:56 AM
Last Post: get2sid
  Help to Plot timeline for intreruption of one line production danut_horincas 2 2,587 Feb-28-2023, 11:48 AM
Last Post: get2sid
  How to plot 2 graphs in one figure? man0s 1 1,400 Apr-25-2022, 09:18 AM
Last Post: Axel_Erfurt
  error bars with dataframe and pandas Hucky 4 4,305 Apr-27-2020, 02:02 AM
Last Post: Hucky
  How to display percentage above the bars in bar graph? WhatsupSmiley 0 8,297 Mar-31-2020, 07:21 PM
Last Post: WhatsupSmiley
  Not able to figure out how to create bar plot on aggregate data - Python darpInd 1 2,330 Mar-30-2020, 11:37 AM
Last Post: jefsummers
  How to create correct scatter plot for PCA? LK91 0 2,141 Dec-11-2019, 07:53 PM
Last Post: LK91
  plotting of graphs mudezda1 2 2,872 Feb-11-2019, 12:44 PM
Last Post: mudezda1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020