2

I am studying financial time series, in the format of pandas series. To compare two series I do a scatterplot, and to visualise the time evolution in the scatterplot I can colour them. This is all fine.

My question relates to the legend showing the colours. I would like the legend to show the date/year that the colour corresponds to, rather than just an the index of the data entry, as now. But I haven't been able to do this, or to find a question like this on stackoverflow.

I know the time series knows the dates, and if you just plot a time series, the x-axis will show the dates.

My code is

from pandas_datareader import data as web
import matplotlib.pyplot as plt
import pandas as pd

#Download data
start = '2010-1-1'
end = '2016-10-1'
AAPL = web.DataReader('AAPL', 'yahoo', start=start, end=end)['Adj Close']
TSLA = web.DataReader('GOOG', 'yahoo', start=start, end=end)['Adj Close']

#Scatterplot
plt.scatter(AAPL, TSLA, alpha=.4, c=range(len(AAPL)))
plt.colorbar()
plt.xlabel("AAPL")
plt.ylabel("TSLA")
plt.grid()
plt.show()

This code produces this plot: Scatterplot with colours and legend

Thanks

1
  • 1
    Welcome to StackOverflow! Minor nitpick: I think the word you're looking for is colorbar, not legend. A legend is normally used to denote a box overlay that labels and explains different datasets plotted together on the same plot. Commented Oct 15, 2016 at 23:21

1 Answer 1

4

While there might be an easier answer out there (anyone?), to me the most straightforward way is to change the colorbar ticks manually.

Try the following before you invoke plt.show():

clb = plt.gci().colorbar # get the colorbar artist
# get the old tick labels (index numbers of the dataframes)
clb_ticks = [int(t.get_text()) for t in clb.ax.yaxis.get_ticklabels()]
# convert the old, index, ticks into year-month-day format
new_ticks = AAPL.index[clb_ticks].strftime("%Y-%m-%d")
clb.ax.yaxis.set_ticklabels(new_ticks)

Note that strftime does not appear to be implemented in pandas versions older than 0.18. In that case you'll have to replace .strftime("%Y-%m-%d") with .apply(lambda x: x.strftime('%Y-%m-%d')) as explained here.

clb-ymd_labels-pandas

Sign up to request clarification or add additional context in comments.

1 Comment

If you think the answer was helpful and informative, consider following the stackoverflow guidelines, marking it as accepted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.