-1

I have a code like

import matplotlib.pyplot as plt
x_axis=(0,10)
y_axis=(0,10)
label = [1,2,3,4,5,6,7]
print(type(label))
plt.figure()
plt.plot(x_axis,y_axis,color='none')
plt.plot([label])
plt.legend(label)
plt.show()

I made the color = 'none' to clear the lines in the plot. But I want to let all labels in legend have color, what should I do? when the label is an int list, code runs will, but when I want to make an str list legend, it not works(because plt.plot([label]), get error:TypeError: unhashable type: 'numpy.ndarray') but if I remove it, the legend only prints the first values. Example Image

martineau
  • 119,623
  • 25
  • 170
  • 301
Ryine
  • 1
  • 2
  • You will probably need to make a custom legend: [How to make custom legend in matplotlib](https://stackoverflow.com/q/13303928/7758804) and [Manually add legend Items Python matplotlib](https://stackoverflow.com/q/39500265/7758804) – Trenton McKinney Aug 06 '21 at 17:59

1 Answers1

0

By definition, that first label matches the first line you plot; the second label matches the second line you plot, and so on. Your output with the "int list" has a "none" colored line for the label '1', just as you specified. To get a visible (non-clear) line for 1, you have to specify something that color -- and it will match the first line.

I recommend that you work through the documentation again, paying close attention to the various formats for plotting. Naming a variable x_axis does not automatically make that the limits for the x-axis: that comes from the plot interface. In this case, you get the line because you specified to plot a line from (0, 0) to (10, 10).

Prune
  • 76,765
  • 14
  • 60
  • 81