1

I am coding a python app which collects temperature and time from a wireless sensor network.

Briefly a snippet of my code is like this:

a.set_facecolor((0.9, 0.9, 1.0))
myFmt = mdates.DateFormatter('%H:%M')
a.xaxis.set_major_formatter(myFmt)
a.xaxis_date()
a.set_title('WSN temperature')
a.set_ylabel('Temp ' + u"\u2103")
a.set_xlabel('Time')
a.grid(True)
a.set_ylim([-20, 40])   #values found in the collectsensor.py
a.yaxis.set_ticks(np.arange(-20, 40, 5))
a.set_xlim([timeIntervalLeft, timeIntervalRight])

cmap = clr.LinearSegmentedColormap.from_list("", ["darkblue", "blue", "violet", "yellow", "orange", "red"])
cmap2 = clr.LinearSegmentedColormap.from_list("", ["red", "orange", "yellow", "violet", "blue", "darkblue"])

a.scatter(timeList, tempList, c=tempList, s=100, cmap=cmap)

y = tempList
s = pd.Series(y, index=timeList)

# convert dates to numbers first
inxval = mdates.date2num(s.index.to_pydatetime())
points = np.array([inxval, s.values]).T.reshape(-1, 1, 2)

segments = np.concatenate([points[:-1], points[1:]], axis=1)

lc = LineCollection(segments, cmap=cmap2, linewidth=3)
lc.set_array(inxval)
a.add_collection(lc)

With that code I obtain something like:

enter image description here

You can see that scatter can make use of cmap, so the dots become "hotter" as the y values increase. I looked for something similar for the plot function but I have not found anything that could work for me.

Thanks to ImportanceOfBeingErnest I noticed that previous question How to plot multi-color line if x-axis is date time index of pandas

But here the color is not really related to the temperature in y axes, but it is distributed along all the cmap color.

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
Condo
  • 83
  • 2
  • 10
  • Do you mean something like [``plt.cm.coolwarm(v)``](https://matplotlib.org/examples/color/colormaps_reference.html) – Jurgy Mar 13 '18 at 15:18
  • https://stackoverflow.com/questions/38208700/matplotlib-plot-lines-with-colors-through-colormap – Dadep Mar 13 '18 at 15:28
  • @Dadep I saw that, but I have only one plot instead of many – Condo Mar 13 '18 at 15:30
  • @Jurgy can you make me an example on how to use it in my code? Thanks – Condo Mar 13 '18 at 15:31
  • In addition to [the duplicate](https://stackoverflow.com/questions/44642966/how-to-plot-multi-color-line-if-x-axis-is-date-time-index-of-pandas) also see [this question](https://stackoverflow.com/questions/47851492/plot-curve-with-blending-line-colors-with-matplotlib-pyplot) for possible optimizations. – ImportanceOfBeingErnest Mar 13 '18 at 15:33
  • @ImportanceOfBeingErnest I am tryng to implement something similar to your first link solution, but the problem is that the gradiend is not in the right way(colder is red) and mostly the rainbow of color is distributed to all segments to cover all the colors of the plasma cmap, but it is not related to the temperature of y axes – Condo Mar 13 '18 at 16:07
  • Feel free to ask a specific question about the problem you face, applying a known solution. In the current question you haven't even tried to use a LineCollection. Also note that such question need to have a [mcve] of the issue present. But first try to understand the linked solution, since you are completely free which array to use for the colors. – ImportanceOfBeingErnest Mar 13 '18 at 16:12
  • 1
    Why are you using `lc.set_array(inxval)`? `inxval` are the dates and has nothing to do with temperature. – ImportanceOfBeingErnest Mar 13 '18 at 16:20
  • @ImportanceOfBeingErnest thanks so much. I was having some trouble because I used a python list instead of a numpy array. Now I solved qith something like: arr = np.array(tempList) lc.set_array(arr) a.add_collection(lc) – Condo Mar 13 '18 at 16:31
  • @ImportanceOfBeingErnest the only thing is that it works good only if points are near together. I do not know why but if I manually add more values the segments keep remaining blue. – Condo Mar 13 '18 at 16:36
  • 2
    Mind that [mcve]s are indispensible when communicating about code. In total this seems to be a different problem. So create a [mcve] of the issue and ask a question about it. – ImportanceOfBeingErnest Mar 13 '18 at 16:49

0 Answers0