Matplot lib-创建具有填充可用空间的点的散点图

问题描述 投票:0回答:1

我可以如下创建散点图:

fig, ax = plt.subplots()
x1 = [1, 1, 2]
y1 = [1, 2, 1]
x2 = [2]
y2 = [2]
ax.scatter(x1, y1, color="red", s=500)
ax.scatter(x2, y2, color="blue", s=500)

给出

enter image description here

我想要的是以下内容(对糟糕的绘画作品表示歉意:]

enter image description here

我正在绘制所有都是整数值的数据,所以它们都在网格上。我希望能够控制散点标记的大小,以便可以在这些点周围留有空白,或者可以将这些点做得足够大,以使它们周围没有留白空间(就像我在油漆图像上方)。

注意-理想情况下,解决方案将在纯matplotlib中,使用文档中建议的OOP接口。

python matplotlib plot customization
1个回答
0
投票
import matplotlib.pyplot as plt

# X and Y coordinates for red circles
red_xs = [1,2,3,4,1,2,3,4,1,2,1,2]
red_ys = [1,1,1,1,2,2,2,2,3,3,4,4]

# X and Y coordinates for blue circles
blu_xs = [3,4,3,4]
blu_ys = [3,3,4,4]

# Plot with a small markersize
markersize = 5
plt.plot(red_xs, red_ys, marker="o", color="r", linestyle="", markersize=markersize)
plt.plot(blu_xs, blu_ys, marker="o", color="b", linestyle="", markersize=markersize)
plt.show()

small markers

# Plot with a large markersize
markersize = 50
plt.plot(red_xs, red_ys, marker="o", color="r", linestyle="", markersize=markersize)
plt.plot(blu_xs, blu_ys, marker="o", color="b", linestyle="", markersize=markersize)
plt.show()

large markers

© www.soinside.com 2019 - 2024. All rights reserved.