![]() We can see that we have a legend with colors but not the variable names. Our first attempt to add legends did not work well. Define Coordinates: Define x-axis and y-axis data coordinates, which are used for data plotting. ![]() For visualization: pyplot from matplotlib and For data creation: NumPy. Plt.savefig("scatterplot_colored_by_variable_legend_first_try_matplotlib_Python.png", The following steps are used to set the color to scatter plot: Define Libraries: Import the important libraries which are required for the creation of the scatter plot. Plt.legend(handles=scatter.legend_elements(), Scatter = plt.scatter(df.culmen_length_mm, In legend(), we specify title and handles by extracting legend elements from the plot. We can try to add legend to the scatterplot colored by a variable, by using legend() function in Matplotlib. Add Color to Scatterplot by variable in Matplotlib Adding legend to Matplotlib scatte plot Note that the scatter plot colored by a variable is missing legend to describe the meaning of the clusters we see. Plt.savefig("scatterplot_point_colored_by_variable_matplotlib_Python.png", We use “c” argument in scatter() function to color data points by species variable in the dataframe.Ĭ=df.species.astype('category').cat.codes)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |