How can I do a matplotlib scatter plot with a categorical x-axis, that allows me to specify the marker and color based on a third variable?

Yudkowsky90

Here is my dataframe: https://i.stack.imgur.com/WJQWg.png

I need a matplotlib scatter plot that has the movie title as the label on the x-axis, in the order given by the 'Order' column. I also want the color of the markers to be determined by the genre of the movie. How can I do this with Matplotlib?

Note that I would ideally like to use the object-oriented approach to matplotlib - i.e. using:

fig, ax = plt.subplots()
sehan2

Since you only specify your x-values and not your y-values, it is difficult to know your exact goal. But you can use the following code to create a scatter plot with categorical data. I chose to plot the 'Order' column as y-values, but you can also modify the code to plot something else or reorder you data as you like. So here you go:

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

data = {'Order': np.arange(4), 'Movie Title': ['a', 'b', 'c', 'd'], 'Genre': ['genre2', 'genre1', 'genre1','genre2']}

df = pd.DataFrame.from_dict(data)

fig, ax = plt.subplots()
ax.scatter(df['Movie Title'], df['Order'],
           c=['blue' if g=='genre1' else 'red' for g in df['Genre']])
plt.show()

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

R Plotly: Sort x-axis by categorical variable for scatter plot

How can I preserve order of axis in scatter plot when using categorical values?

How to specify scatter plot point color in matplotlib pyplot?

Specify color of each point in scatter plot (matplotlib)

Matplotlib: how to plot a line with categorical data on the x-axis?

How do I create a scatter plot with ordered character variables on the x-axis using plot() in r?

With seaborn.facetgrid, how do I specify the color of a mapped scatter plot to reflect the values of a column in the data frame?

Categorical x-axis errorbar plot in matplotlib

Problem when i do a scatter plot with lot of dates in the x axis

Is there a way to make matplotlib scatter plot marker or color according to a discrete variable in a different column?

Can I use scatter function in matplotlib without specifying x axis?

How do I overlay multiple plot types (bar + scatter) in one figure, sharing x-axis

bar plot in r with categorical variable on x axis

Sort categorical x-axis in a seaborn scatter plot

How do I move a half-violin plot (outside), a boxplot (middle), and geom_points (inside) for each categorical variable on the x-axis? ggplot2, R

How can I make the origin of the x-axis and the origin of the y-axis of a plot overlap in matplotlib?

How do I make a categorical variable on x-axis of ggplot geom_line with multiple groups

Matplotlib scatter color by categorical factors

How can I normalize colormap in matplotlib scatter plot?

How can I make a scatter plot colored by density in matplotlib?

How do I make a matplotlib scatter plot square?

Color scatter plot points based on a value in third column?

matplotlib: How to plot multiple scatter plots in one grid where each plot is different color based on a separate list?

matplotlib scatter plot colour as function of third and fourth variable

How to show full scatter point marker on axis using matplotlib?

Matplotlib Ribbon Plot (fill_between) with a Categorical X-Axis

Matplotlib: How can I add an alternating background color when I have dates on the x-axis?

Pandas and Matplotlib - how can I change plot area to fit in more x-axis text?

pandas / matplotlib : How do I show all years on the x-axis of my plot?