# How to get a nice bar chart?

I have

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

edit retag close merge delete

Sort by » oldest newest most voted

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)

more

1

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

( 2020-04-06 13:13:16 +0200 )edit

( 2020-04-06 13:29:44 +0200 )edit

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

( 2020-04-06 13:50:37 +0200 )edit

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)


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)

more

Very good, too. :()

( 2020-04-06 13:50:54 +0200 )edit