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

Pratham_Amitabh
  • 61
  • 2
  • 11

1 Answers1

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".

DavideBrex
  • 2,374
  • 1
  • 10
  • 23
  • 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) – Pratham_Amitabh Jun 17 '20 at 14:10
  • 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) ` – DavideBrex Jun 17 '20 at 14:19
  • 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) – Pratham_Amitabh Jun 17 '20 at 14:33
  • 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? – Pratham_Amitabh Jun 17 '20 at 14:40
  • Maybe you need to sort the rows based on RunTime column before plotting, but without seeing the output is difficult to help you! – DavideBrex Jun 17 '20 at 14:45
  • How do I add an image to the comments? I can't seem to figure it out. The image is saved as a file on my desktop – Pratham_Amitabh Jun 17 '20 at 15:38
  • I'm new to the community, which is why I don't have the option to chat yet, Sorry. Can I mail you the image as an attachment? – Pratham_Amitabh Jun 17 '20 at 15:59
  • Just sent you the mail with all the files attached. Thanks again for your help on this. – Pratham_Amitabh Jun 17 '20 at 16:17
  • It was easier than I thought! Please check the updated answer! and good luck with the rest of the assignment :) – DavideBrex Jun 17 '20 at 16:35