Here's an example of how to create a scatter plot and color the data points based on a categorical variable using matplotlib
and pandas
.
main.py668 chars22 lines
In this example, we have created a Pandas DataFrame with x and y values and a categorical variable called class
. We then create a scatter plot using the ax.scatter
method and pass in the x, y and color vector c. We create the color vector by applying a lambda function to the class
variable which maps the category to the corresponding color from the colors
dictionary. Finally, we add a legend to the plot and display it.
This will give you a scatter plot with the data points colored based on the class
variable.
gistlibby LogSnag