8556732
8556732

Reputation: 311

marker style by third variable

Might seem like a repeat question, but the solution in this post doesn't seem to work for me.

I have a bunch of data I want to plot as lines/curves, and another dataset linked to the curves consisting of XYZ data, where Z represents a labeling variable for the curves.

I've got some example code here with some XY data, and labels for anyone wanting to replicate what I'm doing:

plt.plot(xdata, ydata)
plt.scatter(xlab, ylab, c=lab) # needs a marker function adding
plt.show()

Ideally I want to add some kind of unique marker based on the label values; 0.1,0.5,1,2,3,4,6,8,10,20. The labels are the same for each curve.

I have over 100 curves to plot, so something quick and effective is needed. Any help would be great!

My current solution would be to just split the data by labelling values, and then plot separately for each one (long and messy in my opinion). Figured someone might have a more elegant solution here.

I'm guessing you could do this with a dictionary... but I might need some help doing that!

Cheers, KB

Upvotes: 4

Views: 393

Answers (1)

NMech
NMech

Reputation: 600

Matplotlib does not accepts different markers per plot.

However, a less verbose and more robust solution for large dataset is using the pandas and seaborn library:

Additionally you can use the pandas.cut function to plot bins (Its something I regularly need to produce graphs where I can use a third continuous value as a parameter). The way to use it is :

import pandas as pd
import seaborn as sns
url = 'https://pastebin.com/raw/dwGBLqSb' # url of paste
df = pd.read_csv(url)

sns.scatterplot(data = df, x='labx', y='laby',  style='lab')

and it produces the following example:

enter image description here

If you have something more advanced labelling you could also look at LabelEncoder of Sklearn.


Hopefully, I've edited enough this answer not to offend don't post identical answers to multiple questions. For what is worth, I am not affiliated with seaborn library in any way nor am I trying to promote anything. The only thing I am trying to do is help someone with a similar problem that I've come across and I couldn't find easily a clear answer in SE.

Upvotes: 1

Related Questions