Reputation: 29
Turns out that when trying to plot the same synthetically generated numpy arrays of 2D points (one with 12 data points and another with just 1) in both pyplot and seaborn, I have to change the array dimension of the single-point array so that the program does not yield an error:
points = np.array([[1,2],[5,6],[4,1],[3,8],[7,5],[1,5],[0,8],[4,3],[2,1],[1,7],[3,8],[2,5]])
p = np.array([2.5,2])
import matplotlib.pyplot as plt
plt.style.use("ggplot")
plt.plot(points[:,0], points[:,1],"ro")
plt.plot(p[0],p[1], "bo")
plt.axis([0,7.5,0,8.5]);
With the above code I am able to get the pyplot right. However, if I try in seaborn:
import seaborn as sns
sns.scatterplot(x = points[:,0], y = points[:,1])
sns.scatterplot(x = p[:,0], y = p[:,1])
I get the following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-62-31fe2d015723> in <module>
1 import seaborn as sns
2 sns.scatterplot(x = points[:,0], y = points[:,1])
----> 3 sns.scatterplot(x = p[:,0], y = p[:,1])
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
Nonetheless, if I change the dimension of the p array:
p = np.array([[2.5,2]])
It now plots well in seaborn, but no longer works for plotting with pyplot. It yields the following error:
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-63-f26870ae2995> in <module>
5 plt.style.use("ggplot")
6 plt.plot(points[:,0], points[:,1],"ro")
----> 7 plt.plot(p[0],p[1], "bo")
8 plt.axis([0,7.5,0,8.5]);
IndexError: index 1 is out of bounds for axis 0 with size 1
Does anyone know why this happens?
Upvotes: 0
Views: 598
Reputation: 53
You have an inconsequence in the way you define the point p
.
You can either define p
as a points
array of length = 1:
p = np.array([[2.5,2]])
Then use it in pyplot as follows:
plt.plot(p[:,0],p[:,1], "bo")
and in seaborn:
sns.scatterplot(x = p[:,0], y = p[:,1])
Alternatively, if you really want to have the point p
defined as a 1 dimensional array, just pass it as follows:
plt.plot(p[0], p[1], "bo")
sns.scatterplot(x = [p[0]], y = [p[1]])
Upvotes: 1
Reputation: 13335
Pass it like this:
sns.scatterplot(x = [p[0]], y = [p[1]])
Don't pass the values as scalar type. Pass it as a list of elements.
Upvotes: 0