Python 的 Matplotlib 以错误的顺序绘图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37414916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python's Matplotlib plotting in wrong order
提问by
Basically I have two arrays, one containing the values of the x-axis and the second containing the values of the y-axis. The problem is, when I do
基本上我有两个数组,一个包含 x 轴的值,第二个包含 y 轴的值。问题是,当我这样做
plt.semilogy(out_samp,error_mc)
I get this
我明白了
Which doesn't make any sense. That is because the plot functions plots everything as it encounters in the x array, not caring whether it's sorted in ascending order or not. How can I sort these two arrays so that the x array is sorted by increasing value and the y axis sorted in the same way so that the points are the same but the plot is connected so that it doesn't make this mess?
这没有任何意义。那是因为 plot 函数绘制它在 x 数组中遇到的所有内容,而不关心它是否按升序排序。如何对这两个数组进行排序,以便 x 数组按递增值排序,y 轴以相同方式排序,以便点相同但绘图已连接,以免造成混乱?
Thank you in advance!
先感谢您!
采纳答案by SparkAndShine
Sort by the value of x-axis before plotting. Here is an MWE.
绘图前按 x 轴的值排序。这是一个 MWE。
import itertools
x = [3, 5, 6, 1, 2]
y = [6, 7, 8, 9, 10]
lists = sorted(itertools.izip(*[x, y]))
new_x, new_y = list(itertools.izip(*lists))
# import operator
# new_x = map(operator.itemgetter(0), lists) # [1, 2, 3, 5, 6]
# new_y = map(operator.itemgetter(1), lists) # [9, 10, 6, 7, 8]
# Plot
import matplotlib.pylab as plt
plt.plot(new_x, new_y)
plt.show()
For small data, zip
(as mentioned by other answerers) is enough.
对于小数据,zip
(如其他回答者所述)就足够了。
new_x, new_y = zip(*sorted(zip(x, y)))
The result,
结果,
回答by Hidde
It is easier to zip
, sort and unzip
the two lists of data.
更容易对两个数据列表进行zip
排序和取消zip
。
Example:
例子:
xs = [...]
ys = [...]
xs, ys = zip(*sorted(zip(xs, ys)))
plot(xs, ys)
See the zip documentation here: https://docs.python.org/3.5/library/functions.html#zip
请参阅此处的 zip 文档:https: //docs.python.org/3.5/library/functions.html#zip
回答by Sheldore
An alternative to sort the lists would be to use NumPy arrays and use np.sort()
for sorting. The advantage with using arrays would be a vectorized operation while computing a function like y=f(x). Following is an example of plotting a normal distribution:
对列表进行排序的另一种方法是使用 NumPy 数组并np.sort()
用于排序。使用数组的优点是在计算 y=f(x) 之类的函数时进行矢量化操作。以下是绘制正态分布的示例:
Without using sorted data
不使用排序数据
mu, sigma = 0, 0.1
x = np.random.normal(mu, sigma, 200)
f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) )
plt.plot(x,f, '-bo', ms = 2)
Output 1
输出 1
With using np.sort()This allows straightforwardly using sorted array x
while computing the normal distribution.
使用 np.sort()这允许x
在计算正态分布时直接使用排序数组。
mu, sigma = 0, 0.1
x = np.sort(np.random.normal(mu, sigma, 200))
# or use x = np.random.normal(mu, sigma, 200).sort()
f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) )
plt.plot(x,f, '-bo', ms = 2)
Alternatively if you already have both x and y data unsorted, you may use numpy.argsort
to sort them a posteriori
或者,如果您已经将 x 和 y 数据都未排序,则可以使用numpy.argsort
后验对它们进行排序
mu, sigma = 0, 0.1
x = np.random.normal(mu, sigma, 200)
f = 1/(sigma * np.sqrt(2 * np.pi)) *np.exp( - (x - mu)**2 / (2 * sigma**2) )
plt.plot(np.sort(x), f[np.argsort(x)], '-bo', ms = 2)
Notice that the code above uses sort()
twice: first with np.sort(x)
and then with f[np.argsort(x)]
. The total sort()
invocations can be reduced to one:
请注意,上面的代码使用了sort()
两次:首先是 with np.sort(x)
,然后是 with f[np.argsort(x)]
。总sort()
调用可以减少到一个:
# once you have your x and f...
indices = np.argsort(x)
plt.plot(x[indices], f[indices], '-bo', ms = 2)
In both cases the output is
在这两种情况下,输出都是
Output 2
输出 2
回答by Eular
just do this
就这样做
list=zip(*sorted(zip(*(x,y))))
plt.plot(*list)
sorted function will sort according to the 1st argument i.e x values
sorted 函数将根据第一个参数进行排序,即 x 值
回答by lint
You can convert your arrays to numpy arrays, then use argsort on the first array, take the the array and sort both arrays with the argsort array.
您可以将数组转换为 numpy 数组,然后在第一个数组上使用 argsort,获取数组并使用 argsort 数组对两个数组进行排序。
回答by arjun subramannian
I think you need to sort one array and the other array should also get sorted based on the first array. I got this solution from some other stack overflow question. Most probably this should be your solution.
我认为您需要对一个数组进行排序,而另一个数组也应该根据第一个数组进行排序。我从其他一些堆栈溢出问题中得到了这个解决方案。很可能这应该是您的解决方案。
out_samp,error_mc=zip(*sorted(zip(out_samp,error_mc)))
Now plot those two values, you get a correct graph.
现在绘制这两个值,你会得到一个正确的图形。