Realdeo
Realdeo

Reputation: 450

How do I create a bar chart that starts and ends in a certain range

I created a computer model (just for fun) to predict soccer match result. I ran a computer simulation to predict how many points that a team will gain. I get a list of simulation result for each team.

I want to plot something like confidence interval, but using bar chart.

I considered the following option:

enter image description here

Nate Silver's is too complex, he colored the distribution and vary the size of the percentage. I just want a simple bar chart that plots on a certain range.

I don't want to resort to plot bar stacking like shown here

Upvotes: 2

Views: 3541

Answers (2)

iretex
iretex

Reputation: 69

I was inspired by Bart's answer and while working on a similar use case, I came up with a visual implemented with Plotly library.


start_seconds = d['Start'].apply(lambda x: timedelta(hours=x.hour, minutes=x.minute, seconds=x.second)).apply(lambda x: x.total_seconds())

fig = go.Figure()

fig.add_trace(go.Bar(
    x=d['Date'],
    y=d['Duration'],

    # Convert start_seconds to hours for the base
    base=start_seconds / 3600,  
    hovertemplate='Date: %{x}<br>Duration: %{y} seconds<br>First Activity: %{customdata[0]} hours<br>Last Activity: %{customdata[1]} hours',
    marker_color='blue', 

    # Custom data for 'Start' and 'End' legends on hover data
    customdata=list(zip(d['Start'], d['End'])),)
)

Here's the rendered plot

enter image description here

Upvotes: 0

Bart
Bart

Reputation: 10277

Matplotlib's barh (or bar) is probably suitable for this:

import numpy as np
import matplotlib.pylab as pl

x_mean = np.array([1,   3, 6  ])
x_std  = np.array([0.3, 1, 0.7])
y      = np.array([0,   1,  2 ])

pl.figure()
pl.barh(y, width=2*x_std, left=x_mean-x_std)

The bars have a horizontal width of 2*x_std and start at x_mean-x_std, so the center denotes the mean value.

It's not very pretty (yet), but highly customizable:

enter image description here

Upvotes: 6

Related Questions