通过matplotlib中的子图矩阵绘制线条

您所在的位置:网站首页 matplotlib绘制直线 通过matplotlib中的子图矩阵绘制线条

通过matplotlib中的子图矩阵绘制线条

#通过matplotlib中的子图矩阵绘制线条| 来源: 网络整理| 查看: 265

这里是一个解决方案,它是独立于实际的数字大小,并且与图中一起缩放。

我们使用混合变换可以指定图坐标中线的长度,同时指定左上角子图的坐标轴中的垂直或水平位置。所以垂直线在y方向上的图坐标中从0到1,而在第一个子图的轴坐标中它被绑定到x = 0。 然后,我们还添加一个偏移变换,以点的一半线宽移动它,这样它就可以紧靠轴棘。

enter image description here

from matplotlib import pyplot as plt import matplotlib.lines import matplotlib.transforms as transforms import numpy as np data = np.random.random_sample((10,10)) labels = "Some labels around all the subplots" fig, axarr = plt.subplots(3,3, sharex='col', sharey='row') for i, ax in enumerate(axarr.flatten()): ax.scatter(data[:,i], data[:,i+1]) ax.xaxis.set_label_position('top') for i in range(3): axarr[2-i,0].set_ylabel(labels.split()[i]) axarr[0,i].set_xlabel(labels.split()[i+3]) axarr[2-i,0].set_yticklabels([]) #### Create lines #### lw=4 # linewidth in points #vertical line offset1 = transforms.ScaledTranslation(-lw/72./2., 0, fig.dpi_scale_trans) trans1 = transforms.blended_transform_factory( axarr[0,0].transAxes +offset1, fig.transFigure) l1 = matplotlib.lines.Line2D([0,0], [0, 1], transform=trans1, figure=fig, color="#dd0000", linewidth=4, zorder=0) #horizontal line offset2 = transforms.ScaledTranslation(0,lw/72./2., fig.dpi_scale_trans) trans2 = transforms.blended_transform_factory( fig.transFigure, axarr[0,0].transAxes+offset2) l2 = matplotlib.lines.Line2D([0, 1], [1,1], transform=trans2, figure=fig, color="#dd0000", linewidth=4, zorder=0) #add lines to canvas fig.lines.extend([l1, l2]) plt.show()

这里的缩小版,从中可以看出,线位置适应于该图的大小。

enter image description here



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3