Python Matplotlib半黑半白圆圈

Python Matplotlib半黑半白圆圈,python,matplotlib,Python,Matplotlib,我想在matplotlib绘图的原点放置一个半径为R的半黑半白圆。我知道存在一个,但我不知道如何指定圆的左半部分应该是白色,右半部分应该是黑色。(理想的解决方案是允许我指定圆的方向——例如,我应该能够旋转它,例如顶部可以是白色,底部可以是黑色)。最简单的方法是使用两个楔块。(这不会自动重新缩放轴,但如果您愿意,这很容易添加。) 举个简单的例子: import matplotlib.pyplot as plt from matplotlib.patches import Wedge def ma

我想在matplotlib绘图的原点放置一个半径为R的半黑半白圆。我知道存在一个,但我不知道如何指定圆的左半部分应该是白色,右半部分应该是黑色。(理想的解决方案是允许我指定圆的方向——例如,我应该能够旋转它,例如顶部可以是白色,底部可以是黑色)。

最简单的方法是使用两个
楔块。(这不会自动重新缩放轴,但如果您愿意,这很容易添加。)

举个简单的例子:

import matplotlib.pyplot as plt
from matplotlib.patches import Wedge

def main():
    fig, ax = plt.subplots()
    dual_half_circle((0.5, 0.5), radius=0.3, angle=90, ax=ax)
    ax.axis('equal')
    plt.show()

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]

main()

如果希望变换始终位于原点,可以将变换指定为
ax.transAxes
,然后禁用剪裁

例如


但是,这将使圆的“圆度”取决于轴轮廓的纵横比。(你可以用几种方法来解决这个问题,但它会变得更复杂。让我知道你是否想到了这一点,我可以举一个更详细的例子。)我也可能误解了你的意思“在原点”。如果你有一个带有这个符号的字体,你可以使用unicode半填充圆(U+25D0)。奇怪的是,这不在STIX中(包含在matplotlib中),但我知道它在DejaVu SAN中,所以我将从那里使用它

import matplotlib.pyplot as plt
import matplotlib.font_manager
from numpy import *

path = '/full/path/to/font/DejaVuSans.ttf'
f0 = matplotlib.font_manager.FontProperties()    
f0.set_file(path)

plt.figure()
plt.xlim(-1.2,1.2)
plt.ylim(-1.2,1.2)

for angle in arange(0, 2*pi, 2*pi/10):
    x, y = cos(angle), sin(angle)
    plt.text(x, y, u'\u25D0', fontproperties=f0, rotation=angle*(180/pi), size=30)

plt.show()


你应该可以用两个
Wedge
s来完成,但我很难让事情正常运转…@JoeKington--哦。如果你不能设法让它工作,那对我来说有点泄气;-)(我在这里看到了你的一些matplotlib作品,所以……非常令人印象深刻)谢谢!原来我只是做了些蠢事。两个楔子完美地工作,如果你不添加随机的打字错误的东西!是的。这正是我想要的。谢谢:)没问题!希望有帮助!阿杜:艺术家不适合我。我必须使用添加补丁。这适用于1.4.2,我也在更早的版本中使用过。我尝试了第一个示例,只要ax.axis('equal')在代码中,它就不起作用。matplotlib:2.1.2这很简单,但不能以轴为单位指定圆的大小,这正是我在这里想要的。
import matplotlib.pyplot as plt
import matplotlib.font_manager
from numpy import *

path = '/full/path/to/font/DejaVuSans.ttf'
f0 = matplotlib.font_manager.FontProperties()    
f0.set_file(path)

plt.figure()
plt.xlim(-1.2,1.2)
plt.ylim(-1.2,1.2)

for angle in arange(0, 2*pi, 2*pi/10):
    x, y = cos(angle), sin(angle)
    plt.text(x, y, u'\u25D0', fontproperties=f0, rotation=angle*(180/pi), size=30)

plt.show()