2017-01-16 49 views
3

我試圖繪製我的Raspberry Pi的溫度並在網頁上顯示該圖。這工作相當好。然而,我試圖根據感知的風險對不同區域的情節着色(我不確定它們是否對我的Pi構成風險,但在90°C下運行時我感覺不舒服)。試圖繪製溫度

我使用這個代碼來創建情節:

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) 

這將創建以下情節: Example plot

我不喜歡地區的「硬邊」,但我似乎無法找到讓他們「流動」到彼此的方式。有誰知道如何解決這個問題,或者可以將我指向正確的方向?

+0

您是不是要找'線寬= 0'? – Artyer

+0

您是否想將漸變應用於背景? http://matplotlib.org/examples/pylab_examples/gradient_bar.html –

+0

@Artyer這是一個很好的開始,但它仍然是綠色的,然後突然全黃了。我寧願平穩過渡。 –

回答

2

在從herehere的例子(請告訴他們一些愛太),這聽起來像你想沿着以下線的東西...

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

+0

確實!非常感謝你。 –