如何在网格外绘制时保持 matplotlib 网格受限?
How to keep matplotlib grid constrained while drawing outside of the grid?
我正在尝试在 matplotlib 中绘制以下图像:
我暂时不关心红点或黄圈。我要做的就是用半圆绘制网格。一个关键点是我希望轮廓像图片中那样显示。目前,我的代码是这样的:
import matplotlib.pyplot as plt
N = 5
colors = [(111,187,255),(185,239,133)] # Blue and green
plaquettecolors = ["#b9ef85ff", "#6fbbffe5"] # Green and blue
rowcolor = colors*((N-1)//2)
fig, ax = plt.subplots()
# Build the matrix for the colours
bulkcolors = []
for y in range(N-1):
bulkcolors.extend(rowcolor[::(-1)**y])
greenCircle = plt.Circle(
(y,-0.5 + (1+(-1)**(y+1))*(N-1)/2),
0.5,
color=plaquettecolors[0],
fill=True,
ec="black",
lw = 2,
zorder=0
)
blueCircle = plt.Circle(
(-0.5 + (1+(-1)**y)*(N-1)/2,y),
0.5,
color=plaquettecolors[1],
fill=True,
ec = "black",
lw = 2,
zorder=0)
ax.add_patch(greenCircle)
ax.add_patch(blueCircle)
# Reshape things.
bulkcolors = np.array(bulkcolors)
bulkcolors = bulkcolors.reshape((N-1, N-1,-1))
#ax.axis("off")
ax.matshow(bulkcolors)
plt.xticks(range(-1,N))
plt.yticks(range(-1,N))
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
ax.set_xticks(np.arange(-0.5, N-1, 1))
ax.set_yticks(np.arange(-0.5, N-1, 1))
ax.tick_params(which="major", bottom=False, left=False)
plt.show()
问题是我得到以下信息:
我所希望的是一种删除棋盘图案外的网格线以及数字的方法。任何使这项工作正常进行的提示都将不胜感激!
隐藏网格线并改为绘制 horizontal and vertical 线:
ax.axis("off")
ax.matshow(bulkcolors)
plt.xlim(-1.1, N-.9)
plt.ylim(-1.1, N-.9)
for i in np.arange(-.5, N-1, 1):
plt.hlines(i, -.5, N-1.5, lw=2, color='k')
plt.vlines(i, -.5, N-1.5, lw=2, color='k')
我正在尝试在 matplotlib 中绘制以下图像:
我暂时不关心红点或黄圈。我要做的就是用半圆绘制网格。一个关键点是我希望轮廓像图片中那样显示。目前,我的代码是这样的:
import matplotlib.pyplot as plt
N = 5
colors = [(111,187,255),(185,239,133)] # Blue and green
plaquettecolors = ["#b9ef85ff", "#6fbbffe5"] # Green and blue
rowcolor = colors*((N-1)//2)
fig, ax = plt.subplots()
# Build the matrix for the colours
bulkcolors = []
for y in range(N-1):
bulkcolors.extend(rowcolor[::(-1)**y])
greenCircle = plt.Circle(
(y,-0.5 + (1+(-1)**(y+1))*(N-1)/2),
0.5,
color=plaquettecolors[0],
fill=True,
ec="black",
lw = 2,
zorder=0
)
blueCircle = plt.Circle(
(-0.5 + (1+(-1)**y)*(N-1)/2,y),
0.5,
color=plaquettecolors[1],
fill=True,
ec = "black",
lw = 2,
zorder=0)
ax.add_patch(greenCircle)
ax.add_patch(blueCircle)
# Reshape things.
bulkcolors = np.array(bulkcolors)
bulkcolors = bulkcolors.reshape((N-1, N-1,-1))
#ax.axis("off")
ax.matshow(bulkcolors)
plt.xticks(range(-1,N))
plt.yticks(range(-1,N))
ax.grid(which='major', axis='both', linestyle='-', color='k', linewidth=2)
ax.set_xticks(np.arange(-0.5, N-1, 1))
ax.set_yticks(np.arange(-0.5, N-1, 1))
ax.tick_params(which="major", bottom=False, left=False)
plt.show()
问题是我得到以下信息:
我所希望的是一种删除棋盘图案外的网格线以及数字的方法。任何使这项工作正常进行的提示都将不胜感激!
隐藏网格线并改为绘制 horizontal and vertical 线:
ax.axis("off")
ax.matshow(bulkcolors)
plt.xlim(-1.1, N-.9)
plt.ylim(-1.1, N-.9)
for i in np.arange(-.5, N-1, 1):
plt.hlines(i, -.5, N-1.5, lw=2, color='k')
plt.vlines(i, -.5, N-1.5, lw=2, color='k')