2

So I have the below question that needs to make 3 subplots using the data provided. The problem I have run into is that I need to select a specific genre from within the column 'genre_1', for each of the three subplots. I am unable to figure out how to select the specific data. I have provided an example of what the output should look like.

 from plotly.subplots import make_subplots
    import plotly.graph_objects as go

    movies = {'title_year': {0: 2016, 1: 2016, 2: 2016, 3: 2016, 4:2016},'Title': {0: 'La La Land', 1: 'Zootopia',2: 'Lion',3: 'Arrival', 4: 'Manchester by the Sea'},'Runtime': {0: 128, 1: 108, 2: 118, 3: 116, 4: 137},'IMDb_rating': {0: 8.2, 1: 8.1, 2: 8.1, 3: 8.0, 4: 7.9},'genre_1': {0: 'Action',1: 'Animation',2: 'Biography',3: 'Drama',4: 'Drama'}}

    # Create a subplot, using column, 'genre_1' for three genres - 'Action','Drama','Biography' 

    sub_fig = make_subplots(rows=1, cols=3)

    fig.add_trace(go.Scatter(x='Runtime', y='IMDb_rating',row=1, col=1)
    fig.add_trace(go.Scatter(x='Runtime', y='IMDb_rating',row=1, col=2)
    fig.add_trace(go.Scatter(x='Runtime', y='IMDb_rating',row=1, col=3)

Output

3
  • Please provide a sample of your data (not as picture) Commented Jun 17, 2020 at 12:07
  • Edit your question and paste your dataframe there (see here) Commented Jun 17, 2020 at 12:28
  • @DavideBrex I've added the data Commented Jun 17, 2020 at 12:55

1 Answer 1

1

This should work:

list_genre = list(df.genre_1.unique())
sub_fig = make_subplots(rows=1, cols=len(list_genre), subplot_titles= list_genre)

for i,genre in enumerate(list_genre):
    sub_fig.add_trace(go.Scatter(x = df[df.genre_1==genre]["Runtime"],
                             y=df[df.genre_1==genre]["IMDb_rating"]),row=1, col=i+1)


sub_fig.show()

Output:

enter image description here

EDIT: This is the code that you need:

genres_to_plot = ['Action','Drama','Biography']
subset_movies = movies[movies.genre_1.isin(genres_to_plot)].reset_index(drop=True)

fig = px.scatter(subset_movies, x = "Runtime", y = "IMDb_rating", color = "genre_1", facet_col="genre_1", height = 480, width = 850)
fig.show()

Output figure:

enter image description here

You simply needed to add the parameter facet_col to px.scatter. If you want a bubble plot add size="actor_1_facebook_likes".

Sign up to request clarification or add additional context in comments.

9 Comments

I'm trying to subplot just for genre_1 == 'Action', I'm getting an EOF parsing error for the below code, unable to determine where I've made a mistake sub_fig.add_trace(go.Scatter(x=movies[movies['genre_1'] == 'Action']['Runtime'], y=movies[movies['genre_1'] == 'Action']['IMDb_rating'],row=1, cols=1)
You forgot a round bracket before row=1: sub_fig.add_trace(go.Scatter(x=movies[movies['genre_1'] == 'Action']['Runtime'], y=movies[movies['genre_1'] == 'Action']['IMDb_rating']),row=1, cols=1)
sub_fig.add_trace(go.Scatter(x=movies[movies['genre_1'] == 'Action']['Runtime'], y=movies[movies['genre_1'] == 'Action']['IMDb_rating']),row=1, col=1)
sub_fig.add_trace(go.Scatter(x=movies[movies['genre_1'] == 'Action']['Runtime'], y=movies[movies['genre_1'] == 'Action']['IMDb_rating']),row=1, col=1) The above code won't give me an error, but my scatterplot looks like zig-zag lines, all the datapoints are connected, any idea why?
Maybe you need to sort the rows based on RunTime column before plotting, but without seeing the output is difficult to help you!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.