小编str*_*rix的帖子

matplotlib:子图背景(轴面+标签)颜色[或图形/轴坐标系]

我有一个包含3x2子图的图形,我想在中间的一对子图上设置背景颜色,以便更清楚哪些轴标签属于哪个子图.

facecolor构造子图时的设置仅改变由轴定义的区域的颜色; 刻度线和轴标签仍然被绘制figure.patch.假设没有简单的方法可以做到这一点,我可以在相关实例后面添加一个矩形补丁figure.axes.

经过一些实验,似乎figure.axes[x].get_position()返回Axescoördinates(normalizedcoördinates[0.0-1.0]),但Rectangle()似乎想要显示cocordinates(像素).此代码或多或少有效(ED:交互式,但在输出到png时(使用Agg渲染器),Rectangle的定位完全关闭):

import matplotlib.pyplot as plt
import matplotlib

f = plt.figure()
plt.subplot( 121 )
plt.title( 'model' )
plt.plot( range(5), range(5) )
plt.xlabel( 'x axis' )
plt.ylabel( 'left graph' )
plt.subplot( 122 )
plt.title( 'residuals' )
plt.plot( range(5), range(5) )
plt.xlabel( 'x axis' )
plt.ylabel( 'right graph' )
plt.tight_layout(pad=4)

bb = f.axes[0].get_position().transformed( f.transFigure ).get_points()
bb_pad = (bb[1] - bb[0])*[.20, .10]
bb_offs = bb_pad * [-.25, -.20]
r = matplotlib.patches.Rectangle( bb[0]-bb_pad+bb_offs, …
Run Code Online (Sandbox Code Playgroud)

python matplotlib

2
推荐指数
1
解决办法
2110
查看次数

标签 统计

matplotlib ×1

python ×1