Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting colors using color cycle on date plots using `plot_date()`

I'm currently trying to plot multiple date graphs using matplotlibs plot_date function. One thing I haven't been able to figure out is how to assign each graph a different color automatically (as happens with plot after setting axes.color_cycle in matplotlib.rcParams). Example code:

import datetime as dt
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

values = xrange(1, 13)
dates = [dt.datetime(2013, i, 1, i, 0, 0, 0) for i in values]
mpl.rcParams['axes.color_cycle'] = ['r', 'g']
for i in (0, 1, 2):
    nv = map(lambda k: k+i, values)
    d = mdates.date2num(dates)
    plt.plot_date(d, nv, ls="solid")
plt.show()

This gives me a nice figure with 3 lines in them but they all have the same color. Changing the call to plot_date to just plot results in 3 lines in red and green but unfortunately the labels on the x axis are not useful anymore.

So my question is, is there any way to get the coloring to work with plot_date similarly easy as it does for just plot?

like image 704
Wieland Avatar asked Mar 05 '26 10:03

Wieland


1 Answers

From this discussion in GitHub it came out a good way to solve this issue:

ax.plot_date(d, nv, ls='solid', fmt='')

as @tcaswell explained, this function set fmt='bo' by default, and the user can overwrite this by passing the argument fmt when calling plot_date().

Doing this, the result will be:

enter image description here

like image 56
Saullo G. P. Castro Avatar answered Mar 07 '26 22:03

Saullo G. P. Castro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!