Ask Your Question
0

How to get a nice bar chart?

asked 2020-04-06 03:54:56 -0500

geroyx gravatar image

I have

image description

reset()
import scipy.stats
n = 2000
p = 0.2
binom_dist = scipy.stats.binom(n,p)
bar_chart([binom_dist.pmf(x) for x in range(n)], axes_labels=['$k$','$B(k,{},{})$'.format(n,float(p))])

How could I create something nicer, like

image description

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
1

answered 2020-04-06 06:11:34 -0500

Juanjo gravatar image

You can add a list_plot:

reset()
import scipy.stats
n = 2000
p = 0.2
binom_dist = scipy.stats.binom(n,p)
data = [binom_dist.pmf(x) for x in range(n)]
Layout = {'frame': True, 'axes_labels': ['$k$', f'$B(k,{n},{float(p)})$']}
BarChart = bar_chart([binom_dist.pmf(x) for x in range(n)], **Layout)
ListPlot = list_plot(data, plotjoined=True)
show(BarChart + ListPlot)
edit flag offensive delete link more

Comments

1

You will note that I reuse code from your other question.

Juanjo gravatar imageJuanjo ( 2020-04-06 06:13:16 -0500 )edit

oups, your answer appeared while I was writing mine

Sébastien gravatar imageSébastien ( 2020-04-06 06:29:44 -0500 )edit

Very good. And yes, I note that as well. ;)

geroyx gravatar imagegeroyx ( 2020-04-06 06:50:37 -0500 )edit
1

answered 2020-04-06 06:27:55 -0500

Sébastien gravatar image

I suggest to use list_plot instead of bar_chart

sage: import scipy.stats
sage: n = 2000
sage: p = 0.2
sage: binom_dist = scipy.stats.binom(n, p)
sage: L = [binom_dist.pmf(k) for k in range(n)]
sage: kwds = dict(axes_labels=['$k$',''])
sage: kwds.update(legend_label='$B(k,{},{})$'.format(n, float(p))) 
sage: kwds.update(title='Binomial distribution')
sage: m = n*p
sage: v = n*p*(1-p)
sage: list_plot(L, plotjoined=True, xmin=m-v, xmax=m+v, **kwds)

image description

To add the bars to the plot, you may do:

sage: list_plot(L, plotjoined=True, xmin=m-v, xmax=m+v, **kwds) + bar_chart(L)
edit flag offensive delete link more

Comments

Very good, too. :()

geroyx gravatar imagegeroyx ( 2020-04-06 06:50:54 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2020-04-06 03:54:56 -0500

Seen: 59 times

Last updated: Apr 06