我想在 N x M 的网格上绘制各种颜色和相同大小的圆圈。在 ax,y 位置,可以是一个圆圈或什么都没有。
我希望每列都有 ax 标签(这将是一个星期)和一个 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()
这是结果,我没有注释也没有网格
我应该从子图更改为经典图,以便添加我想要的内容吗?感谢您的帮助!
请尝试以下操作。(我还是不明白网格的东西,所以我把它省略了)
import matplotlib.pyplot as plt
from matplotlib.patches import Wedge
import pandas as pd
import numpy as np
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()
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(figsize=(8,13))
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 = 10, verticalalignment='center', horizontalalignment='center')
for jj in range(0,DF.shape[0]):
plt.annotate(xy =(-1, -1*jj),s= 'subj '+str(jj), fontsize =10, verticalalignment='center', horizontalalignment='right')
ax.set_xlim(-1,10)
ax.set_ylim(-15,3)
plt.axis("off")
plt.plot([-1,10], [-15,3], alpha=0)
plt.tight_layout()
plt.show()
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句