PerroNoob
PerroNoob

Reputation: 895

Draw cross inside circle using patches in matplotlib

How can I draw an X inside the outer circle (top left) in the plot below using Patches? I do not want to plot a data point since the axes limits can change (I'm plotting this inside another figure), or when I tried using text the font looks strange. I have this

f = plt.figure(figsize=(6, 6))
ax = f.add_subplot(111)

box = ax.get_position()
axColor = plt.axes([box.x0 + 0.05, box.y0 + 0.05,
                    0.6, 0.6], projection='polar')

axColor.set_yticks([])
axColor.set_xticks([])
axColor.set_rlim(0, 50)

circle = plt.Circle((0., 0.), 10, transform=axColor.transData._b,
                   edgecolor='k', facecolor='None', linewidth=2)
axColor.add_artist(circle)
circle = plt.Circle((0., 0.), 3, transform=axColor.transData._b, 
                   color='k', linewidth=2)
axColor.add_artist(circle)

circle = plt.Circle((-0.02, 0.9), 0.1, transform=axColor.transAxes,
                   edgecolor='k', facecolor='None', linewidth=2, clip_on=False)
axColor.add_artist(circle)
# Cannot draw an X

And the image is

enter image description here

Upvotes: 1

Views: 4526

Answers (1)

Bolat
Bolat

Reputation: 379

Simple and quick way to draw an x on the plot of yours could be to use plt.scatter. You can make use of the x marker to put the x in the center of the circle you have. Check the following code, it draws the x in the center of the leftmost circle. The coordinates you need to provide for the marker are the same relative coordinates you pass to create the circle.

import matplotlib.pyplot as plt

f = plt.figure(figsize=(6, 6))
ax = f.add_subplot(111)

box = ax.get_position()
axColor = plt.axes([box.x0 + 0.05, box.y0 + 0.05,
                    0.6, 0.6], projection='polar')

axColor.set_yticks([])
axColor.set_xticks([])
axColor.set_rlim(0, 50)

circle = plt.Circle((0., 0.), 10, transform=axColor.transData._b,
                   edgecolor='k', facecolor='None', linewidth=2)
axColor.add_artist(circle)
circle = plt.Circle((0., 0.), 3, transform=axColor.transData._b, 
                   color='k', linewidth=2)
axColor.add_artist(circle)

circle = plt.Circle((-0.02, 0.9), 0.1, transform=axColor.transAxes,
                   edgecolor='k', facecolor='None', linewidth=2, clip_on=False)

axColor.add_artist(circle)


mymarker = plt.scatter(-0.02, 0.9, s=300, c='red', transform=axColor.transAxes, marker='x', clip_on=False)
axColor.add_artist(mymarker)



f.savefig('plot_with_x.png')

I also attached the image here. You can adjust the size of the marker by adjusting the value of s and change the color with c. Hope this helps.

Image with x on the circle

Upvotes: 2

Related Questions