直方图不出现在Python屏幕上

2024-10-05 10:42:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用plotly库在IDLE3上绘制直方图。 这是我的密码:

import pandas as pd
import plotly.express as px

x = [84, 65, 78, 75, 89, 59, 90, 88, 83, 72, 91, 90, 73, 54]
df20 = pd.DataFrame(x, columns = ["x"])
hist20 = px.histogram(df20, x="x")
print(hist20)

但是,打印在shell上的不是直方图,而是:

Figure({
    'data': [{'alignmentgroup': 'True',
              'bingroup': 'x',
              'hovertemplate': 'x=%{x}<br>count=%{y}<extra></extra>',
              'legendgroup': '',
              'marker': {'color': '#636efa'},
              'name': '',
              'offsetgroup': '',
              'orientation': 'v',
              'showlegend': False,
              'type': 'histogram',
              'x': array([84, 65, 78, 75, 89, 59, 90, 88, 83, 72, 91, 90, 73, 54]),
              'xaxis': 'x',
              'yaxis': 'y'}],
    'layout': {'barmode': 'relative',
               'legend': {'tracegroupgap': 0},
               'margin': {'t': 60},
               'template': '...',
               'xaxis': {'anchor': 'y', 'domain': [0.0, 1.0], 'title': {'text': 'x'}},
               'yaxis': {'anchor': 'x', 'domain': [0.0, 1.0], 'title': {'text': 'count'}}}
})

Tags: importdomainascountplotly直方图extrahistogram
1条回答
网友
1楼 · 发布于 2024-10-05 10:42:15

您正在尝试print()直方图对象,而不是调用其show()方法来实际渲染直方图 将print(hist20)更改为hist20.show()

hist20 = px.histogram(df20, x="x")
hist20.show()

这将打开浏览器并显示直方图。如果您想更改输出,也许值得看一下the documentation

相关问题 更多 >

    热门问题