0

EDIT What I was aiming to achieve has been answered in the above post :)

I'm new to Stack Overflow, sorry if I've missed anything or put the wrong tags etc!!

I have a set of model data that I am trying to plot against my own data by having a curve of the model data and a scatter plot of my data points. I'm color-coding the model data, and the colormap works for a scatter plot but when I use it for my line plot it returns the error below. Any help with what's going on and how I can use a colormap on my line plot would be very appreciated!! :)

  Traceback (most recent call last):

  File ~\Documents\Uni\Year 5\Investigation\pdfs\untitled0.py:13 in <module>
    plt.plot(modx,mody,color=cm.hsv(norm(spt))) #this is where the error comes up

  File ~\anaconda3\lib\site-packages\matplotlib\pyplot.py:2757 in plot
    return gca().plot(

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py:1632 in plot
    lines = [*self._get_lines(*args, data=data, **kwargs)]

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:312 in __call__
    yield from self._plot_args(this, kwargs)

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:538 in _plot_args
    return [l[0] for l in result]

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:538 in <listcomp>
    return [l[0] for l in result]

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:531 in <genexpr>
    result = (make_artist(x[:, j % ncx], y[:, j % ncy], kw,

  File ~\anaconda3\lib\site-packages\matplotlib\axes\_base.py:351 in _makeline
    seg = mlines.Line2D(x, y, **kw)

  File ~\anaconda3\lib\site-packages\matplotlib\lines.py:370 in __init__
    self.set_color(color)

  File ~\anaconda3\lib\site-packages\matplotlib\lines.py:1030 in set_color
    mcolors._check_color_like(color=color)

  File ~\anaconda3\lib\site-packages\matplotlib\colors.py:130 in _check_color_like
    raise ValueError(f"{v!r} is not a valid value for {k}")

ValueError: array([[1.00000000e+00, 0.00000000e+00, 0.00000000e+00, 1.00000000e+00],
       [1.00000000e+00, 6.48530060e-01, 0.00000000e+00, 1.00000000e+00],
       [7.02939879e-01, 1.00000000e+00, 0.00000000e+00, 1.00000000e+00],
       [3.12493437e-02, 1.00000000e+00, 1.31250131e-06, 1.00000000e+00],
       [0.00000000e+00, 1.00000000e+00, 6.17278534e-01, 1.00000000e+00],
       [0.00000000e+00, 7.11032029e-01, 1.00000000e+00, 1.00000000e+00],
       [0.00000000e+00, 6.25019688e-02, 1.00000000e+00, 1.00000000e+00],
       [6.09189879e-01, 0.00000000e+00, 1.00000000e+00, 1.00000000e+00],
       [1.00000000e+00, 0.00000000e+00, 7.42280060e-01, 1.00000000e+00],
       [1.00000000e+00, 0.00000000e+00, 9.37500000e-02, 1.00000000e+00]]) is not a valid value for color

MWE:

import matplotlib.pyplot as plt
from matplotlib import cm
import matplotlib.colors as mcolors

spt = [1,2,3,4,5,6,7,8,9,10]
modx = [0.45,0.53,0.56,0.58,0.60,0.63,0.67,0.73,0.79,0.86]
mody = [0.32,0.39,0.44,0.47,0.51,0.54,0.58,0.63,0.67,0.71]
x = [0.98, 0.66, 0.82, 0.63, 0.86, 0.79, 0.67]
y = [0.62, 0.58, 0.48, 0.46, 0.27, 0.67, 0.43]
norm = mcolors.Normalize(vmin=1,vmax=16)

plt.scatter(modx,mody,c=cm.hsv(norm(spt)),marker="o")
plt.plot(modx,mody,color=cm.hsv(norm(spt))) #this is where the error comes up
plt.scatter(x,y,color="k",marker="+")
plt.show()
gemma
  • 1
  • 3
  • What is your desired result here? If a line connects two points with different colors, do you want the line to have one of those colors? Do you want the colormap to continuously evolve along the line? – falafelocelot Aug 02 '22 at 15:15
  • Please include the [full traceback](//realpython.com/python-traceback) when asking about errors - they’re full of valuable debugging details and we (and you) need them to understand what’s going wrong! Thanks :) – Michael Delgado Aug 02 '22 at 15:20
  • See the docs for [`plt.plot`](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.plot.html). It plots a single line if passed two lists, and can only use a single color. – Michael Delgado Aug 02 '22 at 15:29
  • @falafelocelot I'm aiming to achieve the latter - a smooth color change along the line between each point :) – gemma Aug 02 '22 at 15:29
  • @MichaelDelgado I've added the full traceback now :) – gemma Aug 02 '22 at 15:30
  • @MichaelDelgado Is there another way that I can plot a line with a colormap? Matplotlib is the only plotting module that I've ever used and I'm not the best at Python – gemma Aug 02 '22 at 15:33
  • What is it that you’re trying to do? Do you want each segment to be a different color? – Michael Delgado Aug 02 '22 at 16:07

0 Answers0