Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
450 views
in Technique[技术] by (71.8m points)

python - Value Error with color array when slicing values for scatter plot

I want to specify the frequency of markers that are printed in my scatter plot. After being unsuccessful with markevery (other stackoverflow question: Problems with using markevery) I followed the suggestion to slice my values using the notation of x[::5] and y[::5] for every 5th value.

However, now I get a different error. That is,

Traceback (most recent call last):
  File "C:UsersmkupferNASA_SJSU_UARC_workInfoCodingExamplesPythonExamplesX-Y-Value_Plot_Z-SimTime_02_noSectors.py", line 26, in <module>
    timePlot = ax.scatter(x[::5], y[::5], s=50, c=timeList, marker = marker.next(), edgecolors='none', norm=cNorm, cmap = plt.matplotlib.cm.jet) #cm.Spectral_r
  File "C:Python27libsite-packagesmatplotlibaxes.py", line 5715, in scatter
    colors = mcolors.colorConverter.to_rgba_array(c, alpha)
  File "C:Python27libsite-packagesmatplotlibcolors.py", line 380, in to_rgba_array
    raise ValueError("Color array must be two-dimensional")
ValueError: Color array must be two-dimensional

Here is a simplified version of my code:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import matplotlib.lines as lines
from matplotlib import cm
import csv
import itertools
import random

#callSignList = [AMF2052,AMF2052,AMF2052,AMF2052,AMF2052]
xList = random.sample(xrange(100), 100)
yList = random.sample(xrange(100), 100)
timeList = random.sample(xrange(100), 100)

#prepare the plot
fig = plt.figure(figsize=(18,13))
ax = fig.add_subplot(111)

cNorm  = plt.matplotlib.colors.Normalize(vmin=0, vmax=3600)
marker = itertools.cycle(('o', '^', '+', '8', 's', 'p', 'x'))

x = xList
y = yList

timePlot = ax.scatter(x[::5], y[::5], s=50, c=timeList, marker = marker.next(), edgecolors='none', norm=cNorm, cmap = plt.matplotlib.cm.jet) #cm.Spectral_r
fig.subplots_adjust(top=0.90, bottom=0.15, hspace=0.25,)

# Now adding the colorbar
#fig.colorbar(timePlot, shrink=0.5, aspect=10, orientation="horizontal")
cax = fig.add_axes([0.15, 0.06, 0.7, 0.05])
#The numbers in the square brackets of add_axes refer to [left, bottom, width, height],
#where the coordinates are just fractions that go from 0 to 1 of the plotting area.

#ax.colorbar(timePlot)
cbar = fig.colorbar(timePlot, cax, orientation='horizontal')
cbar.set_label('Relative Simulation Time')

plt.show()

Can someone please give me an idea where I made a mistake? Any help is appreciated. Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your colour list should be the same length as your data. So you need to apply the same slice.

timePlot = ax.scatter(x[::5], y[::5], s=50, c=timeList[::5], 
                      marker = marker.next(), edgecolors='none', 
                      norm=cNorm, cmap = plt.matplotlib.cm.jet)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...