52

I am attempting to create a scatter plot. I have a list of numbers from 0 - 17 as well as an array with 18 values. I can plot the data as a line plot but when I try to plot as a scatter, I get an error message I do not understand:

TypeError: ufunc 'sqrt' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

What does this error message mean and how can I get the data to plot as a scatter?

import numpy as np
import matplotlib.pyplot as plt

y = [7316.0, 7453.25, 7518.25, 7711.5, 7448.0, 7210.25, 7416.75, 6960.75, 
     7397.75, 6397.5, 5522.75, 5139.0, 5034.75, 4264.75, 5106.0, 3489.5, 
     4712.0, 4770.0]
x = np.arange(0,18,1)

plt.rcParams['legend.loc'] = 'best'
plt.figure(1)
plt.xlim(0, 20)
plt.ylim(0, 10000)
plt.scatter(x, y, 'r')
plt.show()
cottontail
  • 10,268
  • 18
  • 50
  • 51
Bogdan Janiszewski
  • 2,743
  • 3
  • 20
  • 25

2 Answers2

97

Check the scatter documentation. Third argument is for size of points and should be scalar or array_like. I assume 'r' is for color so do the following:

plt.scatter(x, y, c='r')
gsmafra
  • 2,434
  • 18
  • 26
  • 6
    Thank you this makes sense now. I was reading it but omitting the c= portion as it is unnecessary in normal plots. It seems silly to have different syntax between the two styles. – Bogdan Janiszewski Mar 01 '16 at 22:36
0

The third positional argument of plot() is fmt= which takes strings of the format '[marker][line][color]' so that each of lines, colors, and markers can be formatted in one go. If you omit line, it becomes a scatter plot. For example, the following two plot the same graph:

plt.plot(x, y, 'ro');

plt.scatter(x, y, color='r');
cottontail
  • 10,268
  • 18
  • 50
  • 51