【问题标题】:当 seaborn 正确绘制时,Plotly 库正在绘制空白区域
【发布时间】:2022-01-20 05:35:06
【问题描述】:

我的代码

import seaborn as sns
import plotly.express as px

sns.histplot(df_user_test_session_question_accuracy, x="QuestionAccuracy")


df = df_user_test_session_question_accuracy
fig = px.histogram(df, x="QuestionAccuracy",
                   title='Histogram of QuestionAccuracy ',
                   opacity=0.8,
                   log_y=True, # represent bars with log scale
                   color_discrete_sequence=['indianred'] # color of histogram bars
                   )
fig.show()

在这里,我在单独的内核中同时运行 seaborn 和 plotly plot,seaborn 正在绘制预期的图,但 plotly 只是给出了一个没有图的大空白,它背后的错误是什么 我想使用 plotly 的交互性,任何帮助表示赞赏

【问题讨论】:

  • 为什么不提供数据以进行重现?事实上,没有人有任何回应的动力。
  • 添加截图
  • 我会将链接分享到Colab。我将与您分享 Colab 链接,并在您验证后删除 Colab。

标签: python jupyter-notebook plotly seaborn histogram


【解决方案1】:

seaborn 使用np.histogram()。如果您想要相同的计算框架,请使用它并使用go.Scatter() 进行有效绘图

import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
import pandas as pd
import numpy as np

df_user_test_session_question_accuracy = pd.DataFrame(
    {
        "QuestionId": np.arange(0, 18000),
        "QuestionAccuracy": np.random.uniform(0, 1, 18000),
    }
)

# use consistent number of bins across various plots...
BINS = 25

sns.histplot(df_user_test_session_question_accuracy, x="QuestionAccuracy", bins=BINS)


df = df_user_test_session_question_accuracy
fig = px.histogram(
    df,
    x="QuestionAccuracy",
    title="Histogram of QuestionAccuracy ",
    opacity=0.8,
    nbins=BINS,
    log_y=True,  # represent bars with log scale
    color_discrete_sequence=["indianred"],  # color of histogram bars
)
fig.show()

# use same mechanisim as seaborn to calculate histrogram bins
y, x = np.histogram(df["QuestionAccuracy"], bins=BINS)
x = np.round(x, 2)
go.Figure(go.Scatter(y=y, line_shape="hvh", fill="tozeroy")).update_xaxes(
    tickmode="array",
    tickvals=np.linspace(0, len(x), 6),
    ticktext=np.linspace(x[0], x[-1], 6),
).show()

【讨论】:

    猜你喜欢
    • 2022-01-25
    • 2011-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-25
    • 2016-09-26
    相关资源
    最近更新 更多