You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Matplotlib绘制服装纸样时的缩放比例问题求助

Matplotlib绘制服装纸样时的缩放比例问题求助

我现在正在用Python的Matplotlib制作服装纸样的模板,目前对绘图的效果还挺满意的——需要的功能都能在Matplotlib里找到。

但现在有个非常关键的问题:比例不对。导出PDF后,原本应该是1cm的方格实际只有0.75cm左右,比如图里p0到p1的距离应该是41.5cm,结果导出文档里实际只有31.8cm。

我附了一张绘图局部的截图,下面是我保存PDF的代码,有没有大佬能给点建议呀?

# Marking lines
fig, ax = plt.subplots()

# Setting plot limits and equal aspect ratio
ax.set_xlim(-1, 20)
ax.set_ylim(-2, 85)
ax.set_aspect('equal')

# Add grid lines for 1 cm squares
# To convert from data units to cm units, you need to know the figure size and dpi
fig_width_cm = fig.get_size_inches()[0] * 2.54  # convert from inches to cm
fig_height_cm = fig.get_size_inches()[1] * 2.54

# Assuming a dpi of 300
dpi = 300
x_ticks = np.arange(-1, 20, 1)  
y_ticks = np.arange(-2, 85, 1)  
ax.set_xticks(x_ticks)
ax.set_yticks(y_ticks)

# Showing grid and inverting y-axis
ax.grid(True)
plt.gca().invert_yaxis()

# Define DPI (dots per inch)
dpi = 300


# Convert axis limits to inches
x_range_in_inches = (21 / 2.54)  # Conversion from cm to inches
y_range_in_inches = (87 / 2.54)  # Conversion from cm to inches

# Set the figure size based on the axis limits
fig.set_size_inches(x_range_in_inches, y_range_in_inches, forward=True)

# Save the figure with only the visible area
file_path = 'E:/#DOCUMENTS/#Code/high_resolution_plot.pdf'
fig.savefig(file_path, dpi=dpi, bbox_inches='tight', pad_inches=0.1, transparent=True) 

# Display the plot
plt.show()

plt.close(fig)

我大概理解这和Matplotlib默认用英寸作为单位有关,可能也和导出时的DPI设置有关,但具体怎么调整还是摸不着头绪,求指点!

备注:内容来源于stack exchange,提问作者Sven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.14 13:20:29