使用matplotlib,我如何打印某些东西"实际尺寸"?

时间:2015-04-01 20:27:43

标签: python printing matplotlib plot scaling

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

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

3 个答案:

答案 0 :(得分:2)

考虑这个例子。我用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.

答案 1 :(得分: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()

我们强制轴是我们所知的尺寸,我们使其数据变换与现实世界相匹配,我们可以打印标尺"。

答案 2 :(得分:1)

使用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_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')

结果: Results