【发布时间】:2017-03-10 14:54:38
【问题描述】:
我想在 N x M 的网格上绘制各种颜色和相同大小的圆圈。在 x,y 位置,可以是一个圆圈或什么都不是。
我希望每列都有一个 x 标签(这里是一周)和一个 ylabel(这里是一个主题)。
目前,我找到了一种使用子图绘制圆圈的方法,但我无法获得文本和网格。
这是我绘制圆圈的代码:
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
from plotly.graph_objs import *
from matplotlib.gridspec import GridSpec
def dual_half_circle(center, radius, angle=0, ax=None, colors=('w','k'), **kwargs):
"""
Add two half circles to the axes *ax* (or the current axes) with the
specified facecolors *colors* rotated at *angle* (in degrees).
"""
if ax is None:
ax = plt.gca()
kwargs.update(transform=ax.transAxes, clip_on=False)
theta1, theta2 = angle, angle + 180
w1 = Wedge(center, radius, theta1, theta2, fc=colors[0], **kwargs)
w2 = Wedge(center, radius, theta2, theta1, fc=colors[1], **kwargs)
for wedge in [w1, w2]:
ax.add_artist(wedge)
return [w1, w2]
这是显示所有内容的代码(我删除了颜色条件):
DF = pd.DataFrame(np.random.choice([True, False], size = (15, 10)))
fig, ax = plt.subplots()
for ii in range(0,DF.shape[1]):
for jj in range(0,DF.shape[0]):
if DF[ii][jj]:
dual_half_circle((ii, -1*jj), radius=0.3, colors=('b','g'), angle=90, ax=ax)
ax.axis('equal')
for ii in range(0,DF.shape[1]):
plt.annotate(xy= (ii, 1), s= 'W'+str(ii), fontsize = 100, verticalalignment='center', horizontalalignment='center')
for jj in range(0,DF.shape[0]):
plt.annotate(xy =(-1, -1*jj),s= 'subj '+str(jj), fontsize = 100, verticalalignment='center', horizontalalignment='center')
plt.show()
这是结果,我没有注释也没有网格
我是否应该从子情节更改为经典情节才能添加我想要的内容? 感谢您的帮助!
【问题讨论】:
-
这里有什么理由不显示minimal reproducible example 吗?你说的网格是什么意思?是不是你用
plt.grid()得到的,如果不是,解释你的意思。删除plt.axis("off")这一行时是否得到了想要的结果? -
抱歉,我修改了帖子...
plt.axis("off")允许删除绘图左上角的小网格,这不是我想要的网格。我希望在子图之间有一些水平和垂直线,但不是带有轴的 x,y 网格。
标签: python matplotlib annotations grid subplot