Matplotlib设置特定象限的背景色

2024-10-01 11:39:15 发布

您现在位置:Python中文网/ 问答频道 /正文

Jfreechart中有一个称为setQuadrantPaint的方法,您可以设置绘图中给定量子点的背景颜色。在

你如何在matplotlib中达到同等水平?在

例如

enter image description here


Tags: 方法绘图matplotlib颜色量子水平背景jfreechart
1条回答
网友
1楼 · 发布于 2024-10-01 11:39:15

您可以在背景中使用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)

enter image description here

在绘制数据点后,但在图像之前,将“自动缩放”设置为“假”,确保轴仅缩放到数据点。在

相关问题 更多 >