简体   繁体   中英

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:

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: 在此处输入图像描述

Assuming that the array is 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.

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. For example, the rows in the part of the array visible in the question have first coordinates close to -2000. For this reason, these rows are squished into what looks like a vertical line in the 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.

  2. Alternatively, if you want to plot all points at once, then using the logarithmic scale on the x-axis may help. 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.:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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