在Python中绘制曲线

3

我希望能够绘制特定弧形形状的曲线,以下是我使用特定值所得到的结果(这些值需要使用),但它绘制的是直线。

我还在困扰于以我想要的方式格式化y轴。它是一个对数刻度,我希望它能达到1(如上图)。感谢您的帮助!=)


让我猜猜 - 你想要的曲线形状是基于线性插值底层数值,然后将这些插值数值绘制在对数轴上? - Karl Knechtel
2个回答

2
你在对数坐标图上的线段没有拉伸是因为在顶部和底部的点之间没有其他的点。 对数坐标图 不会使线段弯曲,只是将点放置在不同的刻度上,它们之间的线段仍然是直线。
要改变这种情况,我们需要在点之间添加更多的点。这样就会使结果变得弯曲。"最初的回答"
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.ticker import ScalarFormatter

# Data for plotting
t = [0.0, 62.5, 125.0, 187.5, 250, 312.5, 375, 437.5, 500]
s = [0.1, 0.005, 0.1, 0.005, 0.1, 0.005, 0.1, 0.005, 0.1]

def extendlist(l):
    master = []
    for i in range(len(l)-1):
        x = np.linspace(l[i], l[i+1], 50)
        master.extend(x)
    return master

t = extendlist(t)
s = extendlist(s)

fig, ax = plt.subplots()
ax.semilogy(t, s)

ax.set(xlabel='x axis', ylabel='y axis', title='Stuff')
plt.xlim((0,500))
plt.ylim((0.001, 1))

plt.show()

这将生成你在纸上绘制的图形。

输入图片描述


最初的回答:This will generate the graph you drew on paper.

1
你可以使用 interp1d
import matplotlib.pyplot as plt
import numpy as np
from scipy.interpolate import interp1d

t = [0.0, 62.5, 125.0, 187.5, 250, 312.5, 375, 437.5, 500]
s = [0.1, 0.005, 0.1, 0.005, 0.1, 0.005, 0.1, 0.005, 0.1]
tnew = np.linspace(0, 500, num=1001, endpoint=True)
f = interp1d(t, s)
plt.semilogy(tnew, f(tnew))
plt.ylim((0.001, 1))
plt.show()

resulting plot


1
你的代码里有什么忘了吗?f是什么,interp又在哪里用到了? - ImportanceOfBeingErnest

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接