Storytelling Data Visualization on Exchange Rates - Datetime coordinates issue


I want to look at exchange rates over presidential election cycles with a focus on election years. If I want to plot a vertical line (ax.axvline), do I have to use a datetime value for my desired x-coord if the graph is plotted using datetime values?

I noticed that the posted solution doesn’t have this issue, but on my notebook I have to actually give a datetime value for the x position for ax.text, or ax.axvspan, etc.

I think I attached my file below, sorry incredibly new to computers in general lol.


Basics.ipynb (222.2 KB)

Click here to view the jupyter notebook file in a new tab

Hi and welcome to the Community!
Sorry for the late answer, but in case you haven´t figured it out yet…

It seems to be a matplotlib version issue.
It seems that in newer versions of matplotlib you can´t pass a non-datetime value to an axis that has datetime units, only datetime values are accepted, which is more intuitive and easier to use in my opinion. In older versions, in order to plot ax.axvspan for example it was necessary to convert a date to a numeric value following this logic (from matplotli API):

Matplotlib represents dates using floating point numbers specifying the number of days since 0001-01-01 UTC, plus 1.

You can learn more from the answer on this StackOverFlow question.

I guess that the solution was created using an older version of matplotlib, that´s why they had to use numeric values to plot spans and locate texts like in cell [11]:

### Highlihting the peak of the crisis
ax.axvspan(xmin=13950, xmax=14140, ymin=0.09,
           alpha=0.3, color='grey')

The interesting thing though is that the embedded Jupyter Notebook is using a newer version of matplotlib, 3.2.2 to be exact. So, the same code from the solution would cause an error:

ValueError: view limit minimum -22041.1 is less than 1 and is an invalid Matplotlib date value. 
This often happens if you pass a non-datetime value to an axis that has datetime units

And it seems to be a content issue. Could you please submit a ticket from inside the mission related to the problem, using a question button in the top right of the screen?

P.S.: For your next question, please, don´t forget to leave a screen link to the mission as per the DQ guidelines. You might have got an answer faster if there had been the link available.

1 Like