sprogg - 1 year ago 146

Python Question

I'm trying to plot to numpy arrays both of length 10 against one another:

`mass_frac_plot = plt.figure()`

mass_frac = mass_frac_plot.add_subplot(1, 1, 1)

mass_frac.scatter(mass_frac, homogen_frac)

I get this:

`Traceback (most recent call last):`

File "./plot.py", line 58, in <module>

mass_frac.scatter(mass_frac, homogen_frac)

File "/usr/local/lib/python3.5/site-packages/matplotlib/__init__.py", line 1811, in inner

return func(ax, *args, **kwargs)

File "/usr/local/lib/python3.5/site-packages/matplotlib/axes/_axes.py", line 3836, in scatter

raise ValueError("x and y must be the same size")

Help! They really are both arrays and of length 10...

`print(type(mass_frac), len(mass_frac))`

print(mass_frac)

print(type(homogen_frac), len(homogen_frac))

print(homogen_frac)

Gives this:

`<class 'numpy.ndarray'> 10`

[ 3.67 3.6 4.45 3.74 4.93 4.35 3.89 5.62 4.73 3.83]

<class 'numpy.ndarray'> 10

[ 98.02982123 96.82921968 88.8207858 83.37174016 75.55236146

87.71156752 91.95410515 66.34245085 77.63112123 119.74640558]

Answer Source

Help! They really are both arrays and of length 10...

No, they're not:

```
mass_frac_plot = plt.figure()
mass_frac = mass_frac_plot.add_subplot(1, 1, 1)
^^^^^^^^^
mass_frac.scatter(mass_frac, homogen_frac)
^^^^^^^^^
```

After you execute your second line, `mass_frac`

is clearly no longer an array at all. Instead, it'll be of type

```
>>> type(mass_frac)
<class 'matplotlib.axes._subplots.AxesSubplot'>
```

Change the variable name so you're not clobbering your array.