I'm having some issues plotting a second column from a pandas dataframe onto a twinx y-axis. I think it might be because the second problematic column contains NaN values. The NaN values are there because there was only data available every 10th year, although for the first column there was data available every year. They were generated in using np.nan which I included at the end for clarity.
The intuition here is to plot both series on the same x-axis to show how they trend over time.
Here's my code and dataframe:
import pandas as pd
import numpy as np
import matplotlib as plt
import matplotlib.pyplot as plt
list1 = ['1297606', '1300760', '1303980', '1268987', '1333521', '1328570',
'1328112', '1353671', '1371285', '1396658', '1429247', '1388937',
'1359145', '1330414', '1267415', '1210883', '1221585', '1186039',
'884273', '861789', '857475', '853485', '854122', '848163', '839226',
'820151', '852385', '827609', '825564', '789217', '765651']
list1a = [1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991,
1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
2004, 2005, 2006, 2007, 2008, 2009, 2010]
list3b = [121800016.0, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
145279588.0, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
160515434.5, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan,
168140487.0]
d = {'Year': list1a,'Abortions per Year': list1,
'Affiliation with Religious Institutions': list3b}
newdf = pd.DataFrame(data=d)
newdf.set_index('Year',inplace=True)
fig, ax1 = plt.subplots(figsize=(20,5))
y2min = min(newdf['Affiliation with Religious Institutions'])
y2max = max(newdf['Affiliation with Religious Institutions'])
ax1.plot(newdf['Abortions per Year'])
#ax1.set_xticks(newdf.index)
ax1b = ax1.twinx()
ax1b.set_ylim(y2min*0.8,y2max*1.2)
ax1b.plot(newdf['Affiliation with Religious Institutions'])
plt.show()
I end up with a chart which doesn't show the second plot. (When I changed the second plot to have numeric values for each year, it plots it). Here's the second plot (with NaN values) -- being ignored:
Grateful for any advice.
*how the np.nan values were generated for the second column: I looped thru the index column and for every year without data, returned np.nan to the list, which was then made a column.
for i in range(len(list1a)):
if list1a[i] in list3a:
var = list2[j]
list3b.append(var)
j+=1
else:
var = np.nan
list3b.append(var)


