所以我有以下问题,需要使用提供的数据进行3次细分。我遇到的问题是,我需要从“ genre_1”列中为三个子图的每一个选择特定的流派。我无法弄清楚如何选择特定数据。我提供了输出看起来像的示例。
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)
这应该起作用:
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()
输出:
编辑:这是您需要的代码:
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()
输出图:
您只需要将参数facet_col
添加到px.scatter
。如果需要气泡图,请添加size="actor_1_facebook_likes"
。