简体   繁体   English

我怎样才能从我的情节中删除多余的线?

[英]how can i remove an extra line from my plot?

I want to plot some data:我想绘制一些数据:

u1 = []

for i in range(12):
    u1.append(10)

f = [14.04, 13.31, 12.71, 11.89, 9.92, 8.53, 14.04, 14.92, 15.42, 16.14, 17.42, 20.78]

u2 = [5.8, 4.8, 3.8, 2.8, 1.8, 0.8, 5.8, 4.8, 3.8, 2.8, 1.8, 0.8]

Ku = [round(i/j, 2)*100 for i, j in zip(u2, u1)]

plt.plot(f, Ku)
plt.show()

When I use scatter plot everything seems to work fine:当我使用散点图时,一切似乎都正常:

散点图

However when I use plot function it adds an extra line that connects first point and a maximum point.但是,当我使用 plot 函数时,它会添加一条连接第一个点和最大点的额外线。 But I need only one curve that connects these points .但我只需要一条连接这些点的曲线。

这是我得到的情节

You have to sort the x and y values.您必须对 x 和 y 值进行排序。

x,y = zip(*[(a,b) for a,b in sorted(zip(f,Ku))])
plt.plot(x,y)
plt.show()

If you look at the f list, which you use as your x axis, you'll see that the values are not ordered.如果您查看用作 x 轴的 f 列表,您会看到这些值没有排序。 Matplotlib just follows the line in the order of the points you give it. Matplotlib 只是按照你给它的点的顺序跟随这条线。

You can do the following before the plotting to sort the values in the correct order:您可以在绘图之前执行以下操作以按正确顺序对值进行排序:

import numpy as np
ord = np.flip(np.argsort(f))
f_orderd = np.array(f)[ord]
Ku_ordered =np.array(Ku)[ord]
plt.plot(f_ordered,Ku_ordered); 
plt.show()

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

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