gpt4 book ai didi

python - 如何创建具有精确单位和测量值的图

转载 作者:太空狗 更新时间:2023-10-29 12:25:30 25 4
gpt4 key购买 nike

我正在为大学数学课准备一个事件,我想在向学生介绍整合时使用它。他们将获得一些函数和该函数的绘图。然后他们要计算定积分并用一把剪刀剪掉曲线下的面积。他们将在秤上称这张纸的重量,将显示屏上的值除以特定的纸密度,并将结果与​​之前计算的值进行比较。为了使任务正常进行,绘图中的 1 个单位为 1 实际厘米非常重要。

我尝试用 matplotlib 和 latex 做到这一点。但它并不完全有效。首先,我使用以下 python 代码生成一个图:

import numpy as np
import matplotlib.pyplot as plt


def cm2inches(value):
inch = 2.54
return value/inch

plt.ioff()
#Direct input
plt.rcParams['text.latex.preamble']=[r"\usepackage{lmodern}"]
#Options
params = {'text.usetex' : True,
'font.size' : 10,
'font.family' : 'lmodern',
'text.latex.unicode': True,
}
plt.rcParams.update(params)


def f(x):
return x*x

h = 0.01
x_min = 0.0
x_max = 21.0
X = np.arange(x_min,x_max,h)
Y = f(X)

fig = plt.figure()
ax = fig.add_subplot(111)
plt.xlabel(r'$x$')
plt.ylabel(r'$y$')
ratio = 400./400 #height/width
fig.set_size_inches(cm2inches(x_max),cm2inches(x_max*ratio))
ax.xaxis.set_tick_params(width=0.3)
ax.yaxis.set_tick_params(width=0.3)
for axis in ['top','bottom','left','right']:
ax.spines[axis].set_linewidth(0.3)
plt.subplots_adjust(bottom=0,right=1,top=1,left=0,wspace=0,hspace=0)
plt.axis([x_min,x_max,x_min,x_max])
plt.plot(X,Y,alpha=0.7,lw=0.5)
plt.grid(lw=0.2)
plt.savefig('0.pdf')
fig = plt.close()

文件 0.pdf 大小为 210mm x 210mm。然后我尝试将此文件导入到我的 latex 文档中:

\documentclass[A4,pdftex,10pt]{article}
\usepackage{graphicx}
\usepackage[utf8]{inputenc}
\usepackage[total={210mm,296mm},top=1.6cm,bottom=1cm,left=0cm,right=0cm]{geometry}

\begin{document}

\begin{figure}[H]
\includegraphics{0}
\end{figure}


\end{document}

现在它应该正好是 21 厘米宽。但是右边有个缺口。我该怎么做才能完成这项工作?我必须更改 dpi 吗?

最佳答案

解决了!参见 this线。在上面的代码中我改变了

\documentclass[a4paper,pdftex,10pt]{article}

关于python - 如何创建具有精确单位和测量值的图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42328236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com