Plotly 库正在绘制空白 space 而 seaborn 正在正确绘制

Plotly library is plotting blank space while seaborn is plotting correctly

我的代码

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 只是给出一个没有图的大空白 space,它背后的错误是什么 我想使用 plotly 的交互性,感谢任何帮助

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()