5

enter image description hereFor visual effect purpose, I wish I could remove the grids outside the circle and only keep those are within the circle.

Btw, how to fulfill the cell ([8,9],[9,10]) with red color, I mean, the cell on the right of x=8 and down y=9.

My code is below and current image is also attached.

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.transforms import BlendedGenericTransform

fig, ax = plt.subplots()

ax.text(0, -0.02, 'y', transform=BlendedGenericTransform(ax.transData, ax.transAxes), ha='center')
ax.text(1.01, 0, 'x', transform=BlendedGenericTransform(ax.transAxes, ax.transData), va='center')

ax.set_xticks(np.arange(0,side+1,1))
ax.set_yticks(np.arange(0,side+1,1))
plt.grid()

ax.xaxis.tick_top()
plt.gca().invert_yaxis()

circle = plt.Circle((15, 15), radius=15, fc='w')
plt.gca().add_patch(circle)

fig.set_size_inches(18.5, 10.5)
1
  • Where's the current image? Please check your post after submitting it (or better before). Commented Jul 31, 2015 at 16:45

1 Answer 1

7

The trick is to set the clip_path property on the gridline artists

Here's a simplified (minimal) example:

import numpy as np
import matplotlib.pyplot as plt

fig, ax = plt.subplots()

# draw the circle
circle = plt.Circle((15, 15), radius=15, fc='w')
ax.add_patch(circle)

# settings for the axes
ax.grid()
ax.set_xlim(0,30)
ax.set_ylim(0,30)
ax.set_aspect(1)

# clip the gridlines
plt.setp(ax.xaxis.get_gridlines(), clip_path=circle)
plt.setp(ax.yaxis.get_gridlines(), clip_path=circle)

plt.show()

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

thanks a lot. A follow up question, how to fill a cell, for example ([8,9],[9,10]) with red color? [8,9] are the left and right boundary of x, [9,10] are for y.
just draw a recatngle without boarders.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.