本文介绍了如何在Matplotlib中在绘图框外部绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想按照下图的样式生成子图的标题:
I want to generate the subfigure's title in the style of followed figure:
标题下方应有一个灰色框,该框位于散点的顶部.
A gray box should be beneath the title which are at the top of the scatter point.
这是我尝试过的代码:
x = random.sample(range(50), 50)
y= random.sample(range(50), 50)
fig = pyplot.figure()
ax = pyplot.subplot(111)
ax.scatter(x,y,label='a')
ax.set_aspect('equal')
ax.set_xlim(0,60)
ax.set_ylim(0,60)
ax.plot([0,60], [0, 60], color='k', linestyle='-', linewidth=1.25)
ax.add_patch(patches.Rectangle((0,60),60, 10,facecolor='silver',linewidth = 0))
TITLE = ax.text(26,61, r'$\mathregular{Title}$',fontsize = 14,zorder = 5,color = 'k')
结果显示如下:
任何建议或更好的解决方案都将不胜感激!
Any advice or better solution are appreciate!
推荐答案
我认为,更好的方法是将 clip_on = False 选项用于矩形:
I think a better way is to use the clip_on=False option for Rectangle:
import random
import matplotlib.pyplot as pyplot
x = random.sample(range(50), 50)
y= random.sample(range(50), 50)
fig = pyplot.figure()
ax = pyplot.subplot(111)
ax.scatter(x,y,label='a')
ax.set_aspect('equal')
ax.set_xlim(0,60)
ax.set_ylim(0,60)
ax.plot([0,60], [0, 60], color='k', linestyle='-', linewidth=1.25)
ax.add_patch(pyplot.Rectangle((0,60),60, 10,facecolor='silver',
clip_on=False,linewidth = 0))
TITLE = ax.text(26,61, r'$\mathregular{Title}$',fontsize = 14,zorder = 5,
color = 'k')
pyplot.show()
这将产生一个在轴外绘制的矩形,而不必求助于多余的空间:
This yields a rectangle drawn outside of the axes, without having to resort to extra spaces:
这篇关于如何在Matplotlib中在绘图框外部绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!