How to color a scatter plot by category using Matplotlib in Python

Coloring a scatter plot by category using Matplotlib displays a scatter plot with different color data points for each category.

Solution for How to color a scatter plot by category using Matplotlib in Python : You can use panda.DataFrame.groupby() to plot by category in Matplotlib Call pandas.DataFrame(data) with data as {“a”: x, “b”: y, “c”:z} with x, y, and z as arrays of x-values, y-values and category labels for each data point, respectively, to create a DataFrame with column names “a”, “b”, and “c”. Call pandas.DataFrame.groupby(by) with pandas.DataFrame as the previous result and by as the name of the labels column to group pandas.DataFrame by by. Use the syntax for name, group in groups to iterate through the previous result groups. At each iteration, call matplotlib.pyplot.plot(x, y, marker=”o”, linestyle=””, label=name) with x and y as the x and y columns of group to plot each group separately. Call matplotlib.pyplot.legend() to place a legend on the axes.


how-to-color-a-scatter-plot-by-category-using-matplotlib-in-python