使用matplotlib,如何打印“实际尺寸"? [英] Using matplotlib, how can I print something "actual size"?
问题描述
我有很多图,其中x轴和y轴以厘米为单位,并且我已经在使用axis('equal')来确保适当的宽高比.我想打印出这些图,以便当我测量轴上的距离时,该距离对应于现实世界中的厘米.
I have a number of plots where the x- and y-axes are in centimeter units, and I am already using axis('equal') to ensure proper aspect ratios. I would like to print out those plots so that when I measure distances within my axes, the distances correspond to real-world centimeters.
也就是说,在图中长3单位(厘米)的线应打印为3厘米长. (一个更复杂的示例是在Matplotlib中绘制标尺,然后将其打印出来以供/as/标尺使用.)我在matlab和mathematica中找到了解决方案,但没有找到Matplotlib.有一个神奇的公式可以实现这一目标吗?我相信它将需要以下各项的特殊组合/排序:f igure(figsize=??), axis('equal')
,fig.canvas.draw()
,fig.savefig('filename',format="??")
,可能带有fig.bbox
参数的一些数学运算以及一个或多个dpi设置.我尝试了许多组合,但没有找到正确的组合.也许有一种更简便的方法...
That is, a line that is 3 units (cm) long in the plot should print out to be 3 cm long. (A more complex example would be drawing a ruler in Matplotlib, and then printing it out for use /as/ a ruler.) I found solutions in matlab and mathematica, but not for Matplotlib. Is there a magic formula to achieve this? I believe it will require a special combination/ordering of: figure(figsize=??), axis('equal')
, fig.canvas.draw()
, fig.savefig('filename',format="??")
, possibly some math with fig.bbox
parameters, and perhaps one or more dpi-settings. I have tried many combinations but haven't hit on the right one. And maybe there is a way-easier approach ...
推荐答案
将其添加到@pablo reyes的答案中,检查打印机是否为100%,并且非常接近;
Add this to @pablo reyes' answer, check that the printer is at 100%, and it's pretty close;
ax.set_ylim(0,7)
ax.set_xlim(0,10)
ax.plot([0.5, 1.5],[0.25, 0.25],label='One cm?')
ax.plot([6,6],[1,2], label='One cm?')
ax.legend()
我们强制轴成为我们知道的尺寸,使它的数据转换与现实世界匹配,然后我们可以打印尺子".
we force the axis to be a size we know, we make its data-transform match the real world, and we can "print a ruler".
这篇关于使用matplotlib,如何打印“实际尺寸"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!