创建基于列的子图
Creating column based subplots
所以我有以下问题需要使用提供的数据制作 3 个子图。我遇到的问题 运行 是我需要 select 列 'genre_1' 中的特定流派,用于三个子图的每一个。我无法弄清楚如何 select 具体数据。我已经提供了输出应该是什么样子的示例。
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
这应该有效:
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"
.
所以我有以下问题需要使用提供的数据制作 3 个子图。我遇到的问题 运行 是我需要 select 列 'genre_1' 中的特定流派,用于三个子图的每一个。我无法弄清楚如何 select 具体数据。我已经提供了输出应该是什么样子的示例。
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
这应该有效:
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"
.