程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长

+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python绘图:如何删除不在圆内的网格线?

发布于2019-12-04 14:09     阅读(2060)     评论(0)     点赞(5)     收藏(2)


在此处输入图片说明为了达到视觉效果,我希望可以删除圆外的网格,而仅将其保留在圆内。

顺便说一句,如何用红色填充单元格[[8,9],[9,10]),我的意思是,x = 8右侧和y = 9下方的单元格。

我的代码在下面,并且还附有当前图片。

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)

解决方案


诀窍是将clip_path属性设置为网格艺术家

这是一个简化的(最小)示例:

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()

结果:

在此处输入图片说明



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接:https://www.pythonheidong.com/blog/article/167618/a48a95cc048a84e1aa3d/

来源:python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

5 0
收藏该文
已收藏

评论内容:(最多支持255个字符)