Hey everyone. I'm working on a project with my friend, and mentor, and he's a Python expert. Thus, I wanna use this opportunity to learn more Python. I'm trying, more precisely, to replicate graphs that I have already done in Stata. My question is this: How do I get Python to know that I want to plot the reference line at the year 1989, instead of the index where 1989 appears at? Consider the following code:
The graph may not be constructed quite as "Pythonic", but it does what I want. But, the intervention happened in 1989! Not 1970s-ish. Presumably this is because Python recognizes 1989 as 1970-something on the index, instead of as the variable "Year" that I want for it to be at. How might I get the reference line to be at the correct position, at the year 1989? Perhaps Leonardo Guizzetti or Daniel Schaefer might have thoughts?
Oh and also, if you have any edits you'd suggest to the code itself, like how to make it cleaner/more efficient, I'd appreciate it! I look forward to the day that I'll be fluently bilingual in both Stata and Python.
Code:
cls clear * python: import pandas as pd import matplotlib.pyplot as plt hfont = {'fontname':'Times New Roman'} df = pd.read_csv('https://raw.githubusercontent.com/synth-inference/synthdid/master/data/california_prop99.csv', sep=';', parse_dates=['Year'], index_col='Year') #Imports our data df = df.sort_values(by=['State']) #For some reason it wasn't sorted- changing that df_Cali = df[df['State'] == 'California'] #For now... we only want California. df_Cali.plot(y='PacksPerCapita', color=[(.17, .27, .57)], legend=None) # Our basic plot plt.title('Tobacco Trends', fontsize=14, **hfont) plt.xlabel('Year', fontsize=14, **hfont) plt.xticks(fontsize=14, rotation=45, **hfont) plt.ylabel('Cigarette Sales Per Capita', fontsize=14, **hfont) plt.vlines(x=1989, ymin=40, ymax=140, color='red', label="Proposition 99") # !! The problem of interest. plt.grid() plt.show() end
Oh and also, if you have any edits you'd suggest to the code itself, like how to make it cleaner/more efficient, I'd appreciate it! I look forward to the day that I'll be fluently bilingual in both Stata and Python.
Comment