Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用matplotlib,我如何打印内容;实际尺寸;?_Python_Printing_Matplotlib_Plot_Scaling - Fatal编程技术网

Python 使用matplotlib,我如何打印内容;实际尺寸;?

Python 使用matplotlib,我如何打印内容;实际尺寸;?,python,printing,matplotlib,plot,scaling,Python,Printing,Matplotlib,Plot,Scaling,我有很多图,其中x轴和y轴以厘米为单位,我已经在使用轴(“相等”)来确保适当的纵横比。我想打印出这些图,这样当我测量轴内的距离时,这些距离对应于真实世界的厘米 也就是说,绘图中3个单位(cm)长的线应打印为3 cm长。(一个更复杂的例子是在Matplotlib中绘制标尺,然后打印出来以供使用/as/A标尺。)我在matlab和mathematica中找到了解决方案,但在Matplotlib中没有。是否有一个神奇的公式来实现这一点?我相信它需要一个特殊的组合/顺序:figure(figsize=?

我有很多图,其中x轴和y轴以厘米为单位,我已经在使用轴(“相等”)来确保适当的纵横比。我想打印出这些图,这样当我测量轴内的距离时,这些距离对应于真实世界的厘米


也就是说,绘图中3个单位(cm)长的线应打印为3 cm长。(一个更复杂的例子是在Matplotlib中绘制标尺,然后打印出来以供使用/as/A标尺。)我在matlab和mathematica中找到了解决方案,但在Matplotlib中没有。是否有一个神奇的公式来实现这一点?我相信它需要一个特殊的组合/顺序:f
igure(figsize=??)、axis('equal')
fig.canvas.draw()
fig.savefig('filename',format=“?”)
,可能是一些带有
fig.bbox
参数的数学,可能还有一个或多个dpi设置。我试过很多组合,但没有找到正确的。也许有一种更简单的方法…

考虑一下这个例子。其中,我精确指定了轴的尺寸,单位为cm。 matplotlib以英寸为单位工作,因此我将转换为英寸。然后我还用一个特定的dpi(128)保存它,以便它与我的显示器中的设计尺寸相匹配。当然,每个显示器的情况都不同。我通过反复试验发现了这一点,尽管可能还有其他方法。这是代码:

left_margin = 1.   # cm
right_margin = 1.  # cm
figure_width = 10. # cm
figure_height = 7. # cm
top_margin = 1.    # cm
bottom_margin = 1. # cm

box_width = left_margin + figure_width + right_margin   # cm
box_height = top_margin + figure_height + bottom_margin # cm

cm2inch = 1/2.54 # inch per cm

# specifying the width and the height of the box in inches
fig = figure(figsize=(box_width*cm2inch,box_height*cm2inch))
ax = fig.add_subplot(111)
ax.plot([1,2,3])

fig.subplots_adjust(left   = left_margin / box_width,
                    bottom = bottom_margin / box_height,
                    right  = 1. - right_margin / box_width,
                    top    = 1. - top_margin   / box_height,
                    )
fig.savefig('ten_x_seven_cm.png', dpi=128)
# dpi = 128 is what works in my display for matching the designed dimensions.

将此添加到@pablo reyes'的答案中,检查打印机是否为100%,并且非常接近

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

我们强制轴的大小是我们知道的,我们使其数据转换与现实世界相匹配,我们可以“打印标尺”

另一种使用
fig.add_轴的方法相当准确。我已经包括1厘米网格以及

import matplotlib.pyplot as plt
import matplotlib as mpl

# This example fits a4 paper with 5mm margin printers

# figure settings
figure_width = 28.7 # cm
figure_height = 20 # cm
left_right_margin = 1 # cm
top_bottom_margin = 1 # cm

# Don't change
left   = left_right_margin / figure_width # Percentage from height
bottom = top_bottom_margin / figure_height # Percentage from height
width  = 1 - left*2
height = 1 - bottom*2
cm2inch = 1/2.54 # inch per cm

# specifying the width and the height of the box in inches
fig = plt.figure(figsize=(figure_width*cm2inch,figure_height*cm2inch))
ax = fig.add_axes((left, bottom, width, height))

# limits settings (important)
plt.xlim(0, figure_width * width)
plt.ylim(0, figure_height * height)

# Ticks settings
ax.xaxis.set_major_locator(mpl.ticker.MultipleLocator(5))
ax.xaxis.set_minor_locator(mpl.ticker.MultipleLocator(1))
ax.yaxis.set_major_locator(mpl.ticker.MultipleLocator(5))
ax.yaxis.set_minor_locator(mpl.ticker.MultipleLocator(1))

# Grid settings
ax.grid(color="gray", which="both", linestyle=':', linewidth=0.5)

# your Plot (consider above limits)
ax.plot([1,2,3,5,6,7,8,9,10,12,13,14,15,17])

# save figure ( printing png file had better resolution, pdf was lighter and better on screen)
plt.show()
fig.savefig('A4_grid_cm.png', dpi=1000)
fig.savefig('tA4_grid_cm.pdf')
结果:

我认为缺少的步骤是强制axis对象成为测量图形大小的已知比例,然后设置轴限制,以便数据单位与真实世界的单位匹配。这是在显示器中--它与打印机一起工作吗?(原始问题)如果打开保存的图形并以100%的比例打印,它应该与指定的大小匹配。对于打印机来说,dpi只会影响图像的分辨率;比规模大得多。甚至不是等距的——y单位在纸上是3厘米,x单位是5厘米。这是打印100%的比例,没有自动任何东西,查看实际大小的观众,一切我能想到的。(我认为打印机在这方面大多不可靠;准确的是绘图仪。)您使用什么查看/打印?我使用了MacOSX预览版。然后在打印机菜单中,比例为100%,它完成了工作。另存为pdf还反映了所选的大小。OS X预览,标准桌面打印机。我会试试GIMP。现在我明白了困惑。我更关心的是轴的真实尺寸。有人能写出一个超真实变换,尽可能接近计算机来“打印标尺”吗?t我认为轴必须从图形中获取信息,而图形通常不会传递信息,因此,可能不会,当然是一种痛苦。谢谢大家!在将dpi修改为72后,巴勃罗的解决方案基本有效。在添加cphlewis的文章时,我遇到了一些错误(奇怪?):我的axis([xmin,xmax,ymin,ymax])命令被忽略了。。。set_xlim()和set_ylim()也有同样的问题。。。也许是和axis(“相等”)的奇怪互动?解决方法是绘制所有内容,让matplotlib选择x/y限制,尝试强制它们(未成功),使用ax.get_xlim()和ax.get_ylim()确定实际限制是什么(不是我设置的),从中计算地物宽度和地物高度,然后继续使用Pablo的解决方案。再次感谢!我认为
轴([xo,x1,y0,y1])
是指图形的比例,而不是数据转换。您是否尝试过使用ax.set_xlim
等来代替最初定义的轴大小?此外,如果您在打印时将轴(“相等”)定义为相等,则不需要使用轴(“相等”),因此我将其去掉,以跳过此解决方法。