Home > Net >  Pass labels for legend in matplotlib from a csv file
Pass labels for legend in matplotlib from a csv file

Time:08-26

I have a csv file with a large amount of data that I am plotting as a scatter plot using pandas and matplotlib. Each data point corresponds to a different specimen I have tested. I wanted to assign each specimen a specific color/marker with the legend indicating the name of the specimen that each point corresponds to. In my csv file I have a column for specimen name, a column for force, a column for displacement, the color I assigned, and marker type. I defined this function below.

> def master_plot(df):
>     
> 
>     y = df["Force"]
>     x = df["Displacement"]
>     z = df["Volume"]
>     z1 = z.values.tolist()
>     c1 = df["Color"]
>     c1 = c1.values.tolist()
>     m = df["Marker"]
>     m = m.values.tolist()
> 
>     fig = plt.figure()
>     ax = fig.add_subplot(111)
>     
>     plt.scatter(x,y,c = c1, label = z)    
>     plt.plot( [0,25000],[0,25000])
>     
>     ax.set_aspect('equal', adjustable='box')
>     
>     plt.title("Force vs Displacement")
>     plt.ylabel("Force") 
>     plt.xlabel("Displacement")
>     
>     
>     plt.legend(bbox_to_anchor=(1.05, 1.0), loc='upper left')
> master_plot(df)

I can get the scatter plot to output the colors but I just get a list of the different samples in the legend without the color assignment. I have tried including z as an an argument of plt.legend but that results in no output.

enter image description here

  • Related