gpt4 book ai didi

python - 如何从绘图中完全删除 x 轴(和 y 轴)并使用 Python 或 R 编程在某些点绘制切线?

转载 作者:太空宇宙 更新时间:2023-11-03 13:38:52 25 4
gpt4 key购买 nike

我必须创建一个图,其中轴被抑制,切线按固定间隔绘制,如下图所示。
使用 R 编程,我知道如何抑制刻度线并创建绘图。
但我不知道如何抑制整个轴。

在这里,我需要省略整个 a 轴以及其他轴,例如顶轴和右轴。

我最初的尝试是这样的:

tau <- seq(-5,5,0.01)
a <- 0.4 # a is a constant parameter
sigma <- a*tau # tau is a variable, sigma = a*tau
x <- 1/a*cosh(sigma)
y <- 1/a*sinh(sigma)

# plot
plot(x,y,type="l",xaxt="n",yaxt="n")
abline(h=0,lty=1)

绘图还需要在 a*tau = -1,-0.5, 0, 0.5 和 1 处的点和切线。

我点击的链接如下:
Drawing a Tangent to the Plot and Finding the X-Intercept using R
Lines between certain points in a plot, based on the data? (with R)

所需的情节如下所示:
非常感谢 python 或 R 中的任何建议!! enter image description here

最佳答案

使用 Python,

import numpy as np
import matplotlib.pyplot as plt

tau = np.arange(-5, 5, 0.01)
a = 0.4

sigma = a*tau
x = 1/a*np.cosh(sigma)
y = 1/a*np.sinh(sigma)

fig, ax = plt.subplots()
ax.plot(x, y, c='black')

# approximate the curve by a cubic
dxds = np.poly1d(np.polyfit(sigma, x, 3)).deriv()
dyds = np.poly1d(np.polyfit(sigma, y, 3)).deriv()

xs, ys, dxs, dys = [], [], [], []
for s in np.linspace(-1, 1, 5):
# evaluate the derivative at s
dx = np.polyval(dxds, s)
dy = np.polyval(dyds, s)

# record the x, y location and dx, dy tangent vector associated with s
xi = 1/a*np.cosh(s)
yi = 1/a*np.sinh(s)
xs.append(xi)
ys.append(yi)
dxs.append(dx)
dys.append(dy)
if s == 0:
ax.text(xi-0.75, yi+1.5, '$u$', transform=ax.transData)
ax.annotate('$a^{-1}$',
xy=(xi, yi), xycoords='data',
xytext=(25, -5), textcoords='offset points',
verticalalignment='top', horizontalalignment='left',
arrowprops=dict(arrowstyle='-', shrinkB=7))

ax.quiver(xs, ys, dxs, dys, scale=1.8, color='black', scale_units='xy', angles='xy',
width=0.01)
ax.plot(xs, ys, 'ko')

# http://stackoverflow.com/a/13430772/190597 (lucasg)
ax.set_xlim(-0.1, x.max())
left, right = ax.get_xlim()
low, high = ax.get_ylim()
ax.arrow(0, 0, right, 0, length_includes_head=True, head_width=0.15 )
ax.arrow(0, low, 0, high-low, length_includes_head=True, head_width=0.15 )
ax.text(0.03, 1, '$t$', transform=ax.transAxes)
ax.text(1, 0.47, '$x$', transform=ax.transAxes)

plt.axis('off')
ax.set_aspect('equal')
plt.show()

产量

enter image description here

关于python - 如何从绘图中完全删除 x 轴(和 y 轴)并使用 Python 或 R 编程在某些点绘制切线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35190371/

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