python - matplotllib 图形大小和 xelatex 之间的长度比例不匹配
问题描述
我创建了一个 pythonmatplotlib
图
import matplotlib.pyplot as plt
xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt
fig,ax = plt.subplots(figsize=[xwidth, ywidth])
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
fig.savefig("test.pgf", bbox_inches="tight", pad=0)
我在 XeLaTeX 中确定了xwidth
通孔,必须将其转换为英寸。\the\textwidth
一英寸定义为 72 磅。但是,如果我将图形包含到我的文档中,它与线宽不匹配:
\documentclass{scrartcl}
\usepackage{pgf}
\begin{document}
\begin{figure}
\input{test.pgf}
\end{figure}
Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam\newline
Linewidth: \the\textwidth
\end{document}
我究竟做错了什么?
解决方案
我认为,当您使用 时bbox_inches="tight"
,您会缩短图形以删除空白,并且不要扩展轴。
为我使用constrained_layout = true
或plt.tight_layout()
在乳胶下工作。
import matplotlib.pyplot as plt
xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt
fig,ax = plt.subplots(figsize=[xwidth, ywidth], constrained_layout = True)
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
fig.savefig("test.pdf")
或者
import matplotlib.pyplot as plt
xwidth = 418.25555 / 72 # conversion from in to pt
ywidth = 300 / 72 # conversion from in to pt
fig,ax = plt.subplots(figsize=[xwidth, ywidth])
ax.plot([1,2,3],[1,2,3])
ax.set_ylabel("Y-label")
ax.set_xlabel("X-label")
plt.tight_layout()
fig.savefig("test.pdf")
推荐阅读
- javascript - Node.js - 有没有办法通过索引读取存储中的数组?
- c - 从用户那里获取密码
- database - 如果在 prestashop 1.6 中成功连接后与 DB 通信时出现任何问题,则记录数据库连接
- numpy - 为什么 np.dot 会产生我刚刚从书中复制的属性错误?
- python - 增加matplotlib中图像的显示尺寸
- android - 使用 getDefaultSharedPreferences,遇到 Context 问题
- django - 如何从表 def __str__(self) 打印多个对象:返回 self.title
- apache-kafka - Kafka:加入事件形成多个主题
- javascript - 编译时带有警告。反应
- python - 键入:从有效值列表动态创建文字别名