Search code examples
pythonpandasplotlypython-idle

Histogram not appearing on Python screen


I am trying to plot a histogram on IDLE3 using the plotly library. Here is my code:

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)

However, instead of the histogram, what is printed out on shell was:

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'}}}
})

Solution

  • You're trying to print() the histogram object, instead of calling its show() method, to actually render a histogram change the print(hist20) to hist20.show().

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

    This should open a browser and show the histogram. If you want to change the output maybe it's worth to take a look at the documentation.