matplotlib图背景取决于数据

时间:2018-04-25 18:49:50

标签: python matplotlib background

我试图绘制一些数据,并希望根据数据获得彩色背景。

在下面的示例中,我希望左侧y轴上的data1和data2以及右侧y轴上的data3。这很有效。但另外我尝试根据data3着色背景。

如何格式化数据以使其正常工作?

import matplotlib.pyplot as plt
from datetime import datetime as dt
import matplotlib.dates as md

fig, ax1 = plt.subplots(constrained_layout=True)

data1 = [51.2, 51.2, 51.2, 50.7, 50.7, 50.5, 50.4, 50.7, 50.6]
data2 = [46.5, 46.1, 46.2, 46.3, 46.4, 46.3, 46.2, 46.1, 45.5]
data3 = [ 0.0,  1.0,  1.0,  1.0,  0.0,  0.0,  0.0,  0.0,  0.0]

timestamps = [1524614516, 1524615134, 1524615587, 1524615910, 1524616235, 1524616559, 1524616866, 1524617189, 1524617511]
timestamps_ = [dt.utcfromtimestamp(x) for x in timestamps]

for data in (data1,data2):
    ax1.plot(timestamps_, data, marker='.', linestyle='-')
ax1.set_ylabel("degC")

ax2 = ax1.twinx()
ax2.plot(timestamps_, data3, marker='x', linestyle='-')
ax2.pcolor(ax2.get_xlim(), ax2.get_ylim(), zip(timestamps_, data3), cmap='RdGn', alpha=0.3) 
ax2.set_ylabel("ON OFF")       

ax1.set_title("Mytitle")
for tick in ax1.xaxis.get_major_ticks():
    tick.label1.set_horizontalalignment('right')
    tick.label1.set_rotation(35)
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(xfmt)

plt.show()

错误讯息:

Traceback (most recent call last):
  File "/home/tobias/workspace/python_pyplot_test/main.py", line 25, in <module>
    ax2.pcolor(ax2.get_xlim(), ax2.get_ylim(), zip(timestamps_, data3), cmap='RdGn', alpha=0.3) 
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/__init__.py", line 1855, in inner
    return func(ax, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 5732, in pcolor
    X, Y, C = self._pcolorargs('pcolor', *args, allmatch=False)
  File "/usr/local/lib/python2.7/dist-packages/matplotlib/axes/_axes.py", line 5576, in _pcolorargs
C.shape, Nx, Ny, funcname))
TypeError: Dimensions of C (9, 2) are incompatible with X (2) and/or Y (2); see help(pcolor)

2 个答案:

答案 0 :(得分:1)

这是您想要的最小解决方案:

import matplotlib.pyplot as plt
from datetime import datetime as dt
import matplotlib.dates as md
import numpy as np


data3 = np.array([ 0.0,  1.0,  1.0,  1.0,  0.0,  0.0,  0.0,  0.0,  0.0])

x=np.arange(9)
xp,yp=np.meshgrid(x,data3)
xp=xp.astype(float)-0.5
bgcolor=np.ones(xp.shape)*data3[None,:]

plt.pcolor(xp,yp,bgcolor) 
plt.plot(x, data3, marker='x', linestyle='-')

我取出了第二个轴和所有刻度的东西,因为它们与问题本身无关。

答案 1 :(得分:0)

另一种选择是使用axvspans

enter image description here

使用axvspanpcolor之间的一个区别是,axvspan绘制的垂直跨度(矩形)在y - 方向无限制,而pcolor }矩形不是。因此,如果您使用zoom按钮调整绘图大小,axvspan矩形将拉伸到无穷大(粗略地说),而缩小pcolor矩形将暴露白色区域。这并不是什么大不了的事,只是想到你想知道。

另请注意,如果垂直跨距从第一个数据点开始并延伸到下一个数据点,则data3中的最后一个值永远不会被使用。 (九个数据点产生八个垂直跨度)。但是,如果您将垂直跨度置于数据点周围 - 因此每个数据点都位于跨度的中心,则可以使用data3中的所有9个值。

取消注释下面的注释代码(并注释掉timestamps_lefttimestamps_right的当前定义)以查看差异。

import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime as dt
import matplotlib.dates as md

def topydates(timestamps):
    return [dt.utcfromtimestamp(x) for x in timestamps]
fig, ax1 = plt.subplots(constrained_layout=True)

data1 = [51.2, 51.2, 51.2, 50.7, 50.7, 50.5, 50.4, 50.7, 50.6]
data2 = [46.5, 46.1, 46.2, 46.3, 46.4, 46.3, 46.2, 46.1, 45.5]
data3 = [ 0.0,  1.0,  1.0,  1.0,  0.0,  0.0,  0.0,  0.0,  1.0]

timestamps = np.array([1524614516, 1524615134, 1524615587, 1524615910,
                       1524616235, 1524616559, 1524616866, 1524617189, 1524617511])
timestamps_ = topydates(timestamps)

for data in (data1,data2):
    ax1.plot(timestamps_, data, marker='.', linestyle='-')
ax1.set_ylabel("degC")

ax2 = ax1.twinx()
ax2.plot(timestamps_, data3, marker='x', linestyle='-')

# if you want the axvspans to be centered around the data points
# widths = np.diff(timestamps)
# midpoints = timestamps[:-1] + widths/2.0
# timestamps_left = topydates(np.r_[timestamps[0]-widths[0]/2, midpoints])
# timestamps_right = topydates(np.r_[midpoints, timestamps[-1] + widths[-1]/2.0])
# if you uncomment the code above, then comment-out the line below:
timestamps_left, timestamps_right = timestamps_[:-1], timestamps_[1:]

cmap = plt.get_cmap('RdYlGn')
for left, right, val in zip(timestamps_left, timestamps_right, data3):
    print(left, right)
    color = cmap(val)
    ax2.axvspan(left, right, facecolor=color, alpha=0.3)

ax2.set_ylabel("ON OFF")       

ax1.set_title("Mytitle")
for tick in ax1.xaxis.get_major_ticks():
    tick.label1.set_horizontalalignment('right')
    tick.label1.set_rotation(35)
xfmt = md.DateFormatter('%Y-%m-%d %H:%M:%S')
ax1.xaxis.set_major_formatter(xfmt)

plt.show()