0

I have a vector [1,2,3,4,5,6,7,8,9] and a coloring scheme [0,1,1,1,1,1,0,1,1] and I want to make a simple line plot with colors depending on the color scheme and with dates on the xaxis defined by the timestamp vector [ pd.Timestamp(i,1,1) for i in range(2000,2009)]. This is a toy example. In my data which I can't show here this vector is loaded from somewhere and is in timestamp format.

I used the following answer to generate this code which makes my plot:

import numpy as np
import pylab as pl
from matplotlib import collections  as mc
import matplotlib.dates as mdates
x = [1,2,3,4,5,6,7,8,9]
colors = [0,1,1,1,1,1,0,1,1] 
segments = []
y = x
cols = np.zeros(shape=(len(colors),4))
index = 0
for x1, x2, y1,y2 in zip(x, x[1:], y, y[1:]):
    if colors[index]==0:
       cols[index] = tuple([1,0,0,1])
    else: 
       cols[index] = tuple([0,1,0,1])     
    index += 1
    segments.append([(x1, y1), (x2, y2)])
lc = mc.LineCollection(segments, colors=cols, linewidths=2)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.autoscale()
ax.margins(0.1)
ax.set_xticklabels([ pd.Timestamp(i,1,1) for i in range(2000,2009)])
fig.autofmt_xdate()
pl.show()

enter image description here

but I have a problem formatting the date on the x axis. Adding these two lines before pl.show()

ax.xaxis.set_major_locator(mdates.AutoDateLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))

results in ValueError: ordinal must be >= 1. This doesn't usually happen when the x axis is initially set to be of some dates format, but here the construction is special because of the usage of ax.add_collection(lc).

Can you help me fix this?

Veliko
  • 747
  • 1
  • 9
  • 25
  • 1
    I've already provided two versions of this [here](https://stackoverflow.com/questions/44642966/how-to-plot-multi-color-line-if-x-axis-is-date-time-index-of-pandas/44649053#44649053); but people often have problems with the concept. Still it might be good it you tried that solution, instead of placing random ticklabels on the axis. And then come back if something remains unclear. – ImportanceOfBeingErnest Jun 24 '19 at 19:05
  • Thank you, @ImportanceOfBeingErnest the `date2num` methodology worked for me. What should I do now : answer the question with the solution or close the question? – Veliko Jun 24 '19 at 19:39
  • 1
    Well, I would tend to close it as duplicate of the other question then, unless you needed to create significantly different code, in which case there is again the option that you answer the linked question with a new answer, or if in addition the question is also significantly different, you can answer this question. – ImportanceOfBeingErnest Jun 24 '19 at 19:49
  • O.K., before we close it, do you think we can achieve one of the lines to be *dashed* here? – Veliko Jun 24 '19 at 19:54
  • 1
    Oh well, that's a totally different thing, right? A dashed line as part of a linecollection; might be possible but I would have to think on that a bit myself. I would first search if you find something on that matter, if not, ask a new question. That question would then be independent of date time units. But the distance between points of the collection surely matters, if points are too close, no dashing can happen anyways. – ImportanceOfBeingErnest Jun 24 '19 at 19:58
  • 1
    So [here](https://stackoverflow.com/questions/31544489/two-line-styles-in-legend) is an example of different linestyles in a line collection. – ImportanceOfBeingErnest Jun 25 '19 at 00:24

0 Answers0