使用matplotlib 如何打印“实际大小”

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

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

也就是说,一个 3 个单位 (cm) 长的线在图中应该有一个更容易的方法打印出来是 3 厘米长。(一个更复杂的例子是在 Matplotlib 中绘制一个标尺,然后打印出来使用 / as / a 标尺。) 我在 matlab 和 mathematica 中找到了解决方案,但不是 Matplotlib。是否有一个神奇的公式来实现这一点?我相信它可能需要一个特殊的组合

4

考虑这个例子。其中我在 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.
4

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

我们强迫轴是我们知道的大小,我们使它的数据转换与现实世界相匹配,我们可以“打印一把尺子”。

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: Results

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(342)
使用python在excel中映射
上一篇
Excel将单元格从英语翻译成法语
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(75条)