Dennis Hein
Dennis Hein

Reputation: 267

Trying to plot temperature

I was trying to plot the temperature of my Raspberry Pi and display that plot on a webpage. This works reasonably well. However, I was trying to color different regions of the plot according to the perceived risk (I'm not sure if they actually pose a risk to my Pi, but I wouldn't feel comfortable running it at e.g. 90°C).

I'm using this code to create the plot:

fig = plt.figure()

# color regions
plt.fill([0, 0, len(temps)-1, len(temps)-1], [80, 100, 100, 80], 'r', alpha=0.2, linestyle=None)
plt.fill([0, 0, len(temps)-1, len(temps)-1], [60, 80, 80, 60], 'y', alpha=0.2, linestyle=None)
plt.fill([0, 0, len(temps)-1, len(temps)-1], [0, 60, 60, 0], 'g', alpha=0.2, linestyle=None)

# modify axis
plt.axis([0, len(temps)-1, 0, 100])
plt.xticks([])

# plot and safe
plt.plot(temps, color='k')
plt.savefig(buf, format='png')
plt.close(fig)

This creates the following plot: Example plot

I don't like the "hard edges" of the regions, but I can't seem to find a way to let them "flow" into each other. Does anyone know how to solve this or can point me in the right direction?

Upvotes: 4

Views: 1354

Answers (1)

p-robot
p-robot

Reputation: 4904

Following the examples from here and here (please show them some love too), it sounds like you want something along the lines of the following ...

import numpy as np
from matplotlib import pyplot as plt
import matplotlib.colors as clr

# Construct a colormap
cmap = clr.LinearSegmentedColormap.from_list('cmap for Dennis Hein', 
    [(0, '#ff0000'), (70/100., '#ffff00'), (100/100., '#00ff00')], N=64)

# Generate figure and axes
fig, ax = plt.subplots()

# Limits
xmin, xmax = 0, 100
ymin, ymax = 0, 100

# Fake data
X = [[.0, .0], [1.0, 1.0]]

ax.imshow(X, interpolation='bicubic', cmap = cmap, 
    extent=(xmin, xmax, ymin, ymax), alpha = 0.2)

ax.plot(np.random.normal(loc = 50, scale = 2, size = 101), c = 'k')

plt.show()

enter image description here

Upvotes: 3

Related Questions