I am trying to plot a double y axis graph having dates on the x-axis using matplotlib
The dates are daily but I would like the x-axis labels to show the dates say every 10 days or so.
This works fine on a single y-axis graph as below:
x = z['date']
y = z['volume']
#plt.bar(x,y, 0.2, color = 'blue')
fig, ax=plt.subplots()
ax = plt.subplot(111)
ax.bar(x,y, width= 0.3)
ax.xaxis.set_major_locator(DayLocator(bymonthday = range(1,32), interval =
10))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
plt.xticks(rotation = 45)
plt.show()
But I can't seem to get them displaying with two y-axis. I am trying the below code currently:
Here is a sample of the Dataframe DV:
DV
Out[291]:
volume price Date DayDate
0 9100 6.00 1990 736018.0
1 12796 7.90 1984 736082.0
2 14465 7.56 2007 736088.0
3 15000 7.45 1992 736103.0
# note, the index of the df is actually the same as the date column
example of entries in one row index 2016-02-24, 9107.4540, 6.00, 2016-02-24, 736018.0
Now the code for plotting double axis graph:
fig, ax=plt.subplots()
ax = plt.subplot(111)
x = DV['Date'] # nb ; DV['Date'] = date2num(pd.to_datetime(DV ['Date']).tolist())
y = DV['Volume']
y1 = DV['price']
ax.bar(x,y, width = 0.8)
ax.xaxis.set_major_locator(DayLocator(bymonthday = range(1,32), interval = 10))
ax.xaxis.set_major_formatter(DateFormatter('%Y-%m-%d'))
ax2 = ax.twinx()
ax2.plot(ax.get_xticks(), DV['price'], marker = 'o', linestyle = '-', color
= 'r')
ax2.legend(loc='upper right')
ax.legend(loc='upper left')
ax.set_ylabel('Volume ')
ax2.set_ylabel('price')
ax.set_xlabel('Date')
plt.xticks(rotation = 45)
ax.set_title('daily price v. volume')
ax.set_ylim(0,1.3*DV["volume"].max())
ax2.set_ylim(0,1.3*DV["price"].max())
ax2.grid(False)
plt.autoscale()
plt.show()
Any help would be much appreciated!