简体   繁体   English

在Python中绘制曲线

[英]Plotting Curved Lines in Python

I'd like to plot curved lines of a specific arch like shape, below is how far I've gotten using specific values (these values need to be used) but it plots straight lines. 我想绘制一个特定拱形状的曲线,下面是我使用特定值的距离(这些值需要使用),但它绘制了直线。

I'm also having trouble formatting the y axis the way I want. 我也无法按照我想要的方式格式化y轴。 It's a log scale and I'd like it to go up to 1 (like in the ideal plot above). 这是一个对数刻度,我希望它达到1(就像上面的理想情节一样)。 Some help would be great, thanks! 一些帮助会很棒,谢谢! =) =)

The reason why your line is not stretching on a log scale plot is because there are no points between the points that are on the top and on the bottom. 您的线未在对数刻度图上拉伸的原因是因为顶部和底部之间的点之间没有点。 log plot does not curve the lines, only place the points on a different scale, the line between them are still straight. log plot不会对线进行曲线,只将点放在不同的比例上,它们之间的线仍然是直的。

To change this, we add more points between dots. 要改变这一点,我们在点之间添加更多点。 and the result will become curved. 结果会变得弯曲。

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 what you graphed on paper. 这将生成您在纸上绘制的内容。

在此输入图像描述

you can use interp1d 你可以使用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()

结果情节

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM