Home > OS >  python : How to add different markers to different Y values
python : How to add different markers to different Y values

Time:11-13

I'm trying to visualize data where each X value has multiple Y values and I would like to distinguish each Y value visaully. This is the example code

xLables = ['A1','A2','A3','A4','A5']

YValues = [[1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
X = [xLables[i] for i, data in enumerate(YValues) for j in range(len(data))]
Y = [val for data in YValues for val in data]

plt.scatter(X, Y)
plt.grid()
plt.show()

When I plot this , I get the following attached

enter image description here

Each X label has corresponding Y values ... For Ex: A1 has 1,2,3,4 , A2 has 1,2,3,4,5,6,7 and so on

I have two questions on this one

(1) Can I have different markers for different Y-values .. all 1's are stars , all 2's are diamonds , all 10's are circles ?

something like this may be

enter image description here

(2) Is there a better way to plot such 2D data and distingush them where each X has multiple Y values

Any suggestions/help is highly appreciated

Thanks

I tried to add markers and different colors , but they apply to all Y values for each X .. but not specific to each Y values..

CodePudding user response:

Add your marker types to list & itterate over them accordingly.

from matplotlib import pyplot as plt
import matplotlib

xLables = ['A1','A2','A3','A4','A5']

YValues = [[1,2,3,4],[1,2,3,4,5,6,7],[1,2,3],[5,6,7],[1,2,3]]
X = [xLables[i] for i, data in enumerate(YValues) for j in range(len(data))]
Y = [val for data in YValues for val in data]



plt.scatter(X, Y, marker=matplotlib.markers.CARETDOWNBASE)
plt.grid()

markers=['8',' ', '.', 'o', '*','^', 's', 'p', 'h','8',' ', '.', 'o', '*','^', 's', 'p', 'h' ]
for i in range(18):
    plt.plot(X[i], Y[i],  marker=markers[i])
    plt.xlabel('X Label')
    plt.ylabel('Y Label') 
plt.show()

output #

enter image description here

CodePudding user response:

You can add markers. A list of them can be found here: https://matplotlib.org/stable/api/markers_api.html

I think you can use it like this: plt.scatter([1, 2, 3], marker=11) which means that you'll have to put the values you want to be the same in the same list. I don't think there is a way of giving a list of markers or something like that.

As far as i understand your code would look something like:

plt.scatter(X, Y[0], marker=1)
plt.scatter(X, Y[1], marker=1)
plt.scatter(X, Y[2], marker=1)
plt.scatter(X, Y[3], marker=1)

You could make this a for loop if it is something having to work for different sizes but i suppose you'll figure it out from here.

Good luck i hope this helps.

  • Related