Matplotlib - 设置特定象限的背景颜色
问题描述:
在Jfreechart
中有一种叫做setQuadrantPaint
的方法,可以让你设置一个图中给定quandrant的背景颜色。Matplotlib - 设置特定象限的背景颜色
您如何达到matplotlib
的等价物?
E.g.
答
您可以绘制在后台imshow一个2x2的阵列。给它一个程度会使它的中心总是在0,0。
import numpy as np
import matplotlib.pyplot as plt
x1, y1 = np.random.randint(-8,8,5), np.random.randint(-8,8,5)
x2, y2 = np.random.randint(-8,8,5), np.random.randint(-8,8,5)
vmax = np.abs(np.concatenate([x1,x2,y1,y2])).max() + 5
extent = [vmax*-1,vmax, vmax*-1,vmax]
arr = np.array([[1,0],[0,1]])
fig, ax = plt.subplots(1,1)
ax.scatter(x1,y1, marker='s', s=30, c='r', edgecolors='red', lw=1)
ax.scatter(x2,y2, marker='s', s=30, c='none', edgecolors='red', lw=1)
ax.autoscale(False)
ax.imshow(arr, extent=extent, cmap=plt.cm.Greys, interpolation='none', alpha=.1)
ax.axhline(0, color='grey')
ax.grid(True)
数据点后设置自动缩放为False绘制,但在此之前的图像,可确保轴鳞只需要数据点。