简体   繁体   中英

Histogram of the frequency of an occurrence in Plotly with Python

I would like to create a histogram with the y values representing the number of occurrences of a set and the x axis representing the number of sets. For example...

  • Passengers on a ship...
  • Age 0-20: 14
  • Age 21-40: 27
  • Age 41-60: 19

Ideally, there would be three bars (bins) associated with each set. Here is the code I've come up with...

# Bins (x-axis)
bins = [1,2,3]

# Trace (y-axis)
passengers = [14, 27, 19]

# Data
data = [go.Histogram(x=bins, y=passengers)]

# Layout
layout = Layout(title='Histogram Example')      
    
# Figure
fig = {'data': data, 'layout': layout}

#Plot
iplot(fig)

What is the simplest way to represent this data with Plotly's go.Histogram function?

I have created a graph using your data using the official reference. See here .

import plotly.graph_objects as go

bins = ["Age0-20","Age21-40","Age41-60"]
passengers = ["14","27","19"]

fig = go.Figure()

fig.add_trace(go.Histogram(histfunc="sum", y=passengers, x=bins, name="sum"))

fig.update_layout(showlegend=True)
fig.show()

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM