使用matplotlib,如何打印“实际尺寸”?

问题描述 投票:4回答:3

我有很多图,其中x轴和y轴以厘米为单位,并且我已经在使用axis('equal')来确保适当的宽高比。我想打印出这些图,以便当我测量轴内的距离时,该距离对应于现实世界的厘米。

即,图中的3厘米长的线应打印为3厘米长。 (一个更复杂的示例是在Matplotlib中绘制标尺,然后将其打印出来以供/ as /标尺使用。)我在matlab和mathematica中找到了解决方案,但没有找到Matplotlib。有一个神奇的公式可以实现这一目标吗?我相信它将需要以下各项的特殊组合/顺序:f igure(figsize=??), axis('equal')fig.canvas.draw()fig.savefig('filename',format="??"),可能带有fig.bbox参数的一些数学运算以及一个或多个dpi设置。我尝试了很多组合,但没有找到合适的组合。也许有一种更简便的方法...

python printing matplotlib plot scaling
3个回答
2
投票

考虑此示例。我以厘米为单位精确指定轴的尺寸。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.

2
投票

将此添加到@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()

我们强制将轴设为我们知道的尺寸,使它的数据转换与现实世界相匹配,然后我们可以“打印尺子”。


0
投票

使用fig.add_axes的另一种方法非常准确。我也包括了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_magrin = 1 # cm
top_bottom_margin = 1 # cm

# Don't change
left   = left_right_magrin / 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')

结果:enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.