Dash/plotly,仅显示直方图中前 10 个值

Ant*_*sch 7 python plotly plotly-dash

我正在为大学课程创建仪表板。我创建了 3 个直方图,但是,有许多独特的值给出了很长的 x 值范围。在我的图中,我只想显示计数最高的 10 个或 20 个值(前 10 个值)。有人可以帮我吗?

import plotly.express as px
from jupyter_dash import JupyterDash
import dash_core_components as dcc
import dash_html_components as html
from dash.dependencies import Input, Output

# Build App
app = JupyterDash(__name__)
app.layout = html.Div([
   html.H1("forensics "),
   dcc.Graph(id='graph'),
   dcc.Graph(id='graph1'),
   dcc.Graph(id='graph2'),
   html.Label([
        "select market",
        dcc.Dropdown(
            id='market', clearable=False,
            value='whitehousemarket', options=[
                {'label': c, 'value': c}
                for c in posts['marketextract'].unique()
            ])
    ]),
])
# Define callback to update graph
@app.callback(
    Output('graph', 'figure'),
    Output('graph1', 'figure'),
    Output('graph2', 'figure'),
    [Input("market", "value")]
)
def update_figure(market):
    fig=px.histogram(x=posts['datetime'].loc[posts['marketextract']==market])
    fig1=px.histogram(x=posts['username'].loc[posts['marketextract']==market])
    fig2=px.histogram(x=posts['drugs'].loc[posts['marketextract']==market])
    return [fig, fig1, fig2]



# Run app and display result inline in the notebook
app.run_server(mode='inline')
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

ves*_*and 5

据我所知,px.histogram()没有一种方法可以排除垃圾箱的某些观察结果。但从数据的外观来看(请考虑共享适当的示例),您在这里所做的只是显示某些用户名的不同计数。df.groupby()您可以通过和的组合轻松做到这一点px.histogram。或者px.bar()go.Bar()就此而言,但我们会坚持下去,px.histogram因为这就是您寻求帮助的原因。无论如何,使用随机选择的国家/地区名称px.gapminder可以使用:

dfg = df.groupby(['name']).size().to_frame().sort_values([0], ascending = False).head(10).reset_index()
fig = px.histogram(dfg, x='name', y = 'count')
Run Code Online (Sandbox Code Playgroud)

并得到:

在此输入图像描述

如果你放弃,.head(10)你会得到这个:

在此输入图像描述

我希望这就是您正在寻找的功能。并且不要被长的吓倒df.groupby(['name']).size().to_frame().sort_values([0], ascending = False).reset_index()。我不是专家,所以你很可能找到一种更有效的方法。但它确实能完成工作。这是带有一些示例数据的完整代码:

# imports
import pandas as pd
import plotly.express as px
import random

# data sample
gapminder = list(set(px.data.gapminder()['country']))[1:20]
names = random.choices(gapminder, k=100)

# data munging
df = pd.DataFrame({'name':names})
dfg = df.groupby(['name']).size().to_frame().sort_values([0], ascending = False).reset_index()
dfg.columns = ['name', 'count']

# plotly
fig = px.histogram(dfg, x='name', y = 'count')
fig.layout.yaxis.title.text = 'count'
fig.show()
Run Code Online (Sandbox Code Playgroud)