如何将用 Plotly 编写的 Boxplot 代码转换为 Seaborn?

问题描述 投票:0回答:1

有人可以帮我在 Seaborn 中编写以下代码吗?

import plotly

plotly.offline.init_notebook_mode(connected=True)

import plotly.graph_objs as go

trace0 = go.Box(y = apps[apps['Type'] == 'Paid']['Installs'],name = 'Paid')

trace1 = go.Box(y = apps[apps['Type'] == 'Free']['Installs'],name = 'Free')

layout = go.Layout(title = "Number of downloads of paid apps vs. free apps",yaxis = dict(title = "Log number of downloads",type = 'log',
autorange = True))

data = [trace0, trace1]

plotly.offline.iplot({'data': data, 'layout': layout})
python matplotlib plotly seaborn boxplot
1个回答
0
投票

希望这对您有帮助。在不知道数据分布的情况下,我使用了 seaborn 中包含的示例数据集。正如下面的代码在注释行中所示,您可以注释/取消注释以从此示例数据切换到位于数据框apps中的您自己的数据。

import matplotlib.pyplot as plt
import seaborn as sns

# comment line below when using your data
df = sns.load_dataset("titanic")

# uncomment 4 lines below when using your data
# box_plot = sns.boxplot(data=[
#     apps[apps['Type'] == 'Paid']['Installs'],
#     apps[apps['Type'] == 'Free']['Installs']],
#     orient="v")

# comment 4 lines below hen using your data
box_plot = sns.boxplot(data=[
    df[df['sex'] == 'male']['age'],
    df[df['sex'] == 'female']['age']],
    orient="v")

box_plot.set_title('Number of downloads of paid apps vs. free apps')

box_plot.set_yscale('log')

box_plot.set(ylabel="Log number of downloads")

labels = ['Paid', 'Free']

box_plot.set_xticks([i for i in range(2)], labels)

plt.show()

© www.soinside.com 2019 - 2024. All rights reserved.