简体   繁体   English

如何在 Python 中分散 plot 二维数组

[英]How to scatter plot 2d array in Python

How do you plot a scatter plot for an array result_array of shape (1087, 2) that looks like this:你如何 plot 一个散点 plot 的数组result_array的形状(1087, 2)看起来像这样:

array([[-1.89707840e+03,  3.99819932e+00],
       [-2.55018840e+03, -2.61913223e+00],
       [-1.85480840e+03, -2.36545732e-01],
       ...,
       [-1.64432840e+03,  9.79555441e+00],
       [-1.59022840e+03,  1.08955493e+01],
       [-1.73963840e+03,  3.60132161e-01]])

? ?


Update:更新:

Tried:试过:

import matplotlib.pyplot as plt
plt.scatter(result_array[:, 0], result_array[:, 1])
plt.show()

and the plot looks like this: plot 看起来像这样: 在此处输入图像描述

Assuming that the array is X :假设数组是X

import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1])
plt.show()

plt.scatter() has many addional options, see the documentation for details. plt.scatter()有许多附加选项,请参阅文档了解详细信息。

Answer to the updated question:回答更新的问题:

It seems that you have an outlier row in the array with the first coordinate close to 2.5*10^6 (which gives the point close to the right margin of the plot), while other rows have their first coordinates smaller by a few orders of magnitude.似乎您在数组中有一个异常值行,其第一个坐标接近 2.5*10^6(这使得该点接近绘图的右边距),而其他行的第一个坐标小几个数量级震级。 For example, the rows in the part of the array visible in the question have first coordinates close to -2000.例如,问题中可见的数组部分中的行的第一个坐标接近 -2000。 For this reason, these rows are squished into what looks like a vertical line in the plot.出于这个原因,这些行在 plot 中被挤压成看起来像一条垂直线。

There are two possible ways to fix it:有两种可能的修复方法:

  1. If you really have only one (or just a few) outliers, you can remove them from the array and possibly plot them separately.如果您确实只有一个(或只有几个)异常值,您可以将它们从数组中删除,也可以将它们分别从 plot 中删除。

  2. Alternatively, if you want to plot all points at once, then using the logarithmic scale on the x-axis may help.或者,如果您想一次 plot 所有点,那么使用 x 轴上的对数刻度可能会有所帮助。 Since you have some points with negative first coordinates, you would need to use the symmetric logarithmic scale - which is logarithmic in both positive and negative directions of the x-axis.:由于您有一些点的第一坐标为负,因此您需要使用对称对数刻度 - 它在 x 轴的正方向和负方向上都是对数的。:

import matplotlib.pyplot as plt
plt.scatter(X[:, 0], X[:, 1])
plt.xscale('symlog')
plt.show()

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

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