12

I have a set of numbers that I'd like to plot on a histogram.

Say:

import numpy as np
import matplotlib.pyplot as plt

my_numbers = np.random.normal(size = 1000)
plt.hist(my_numbers)

If I want to control the size and range of the bins I could do this:

plt.hist(my_numbers, bins=np.arange(-4,4.5,0.5))

Now, if I want to plot a histogram in Altair the code below will do, but how do I control the size and range of the bins in Altair?

import pandas as pd
import altair as alt

my_numbers_df = pd.DataFrame.from_dict({'Integers': my_numbers})

alt.Chart(my_numbers_df).mark_bar().encode(
    alt.X("Integers", bin = True),
    y = 'count()',
)

I have searched Altair's docs but all their explanations and sample charts (that I could find) just said bin = True with no further modification.

Appreciate any pointers :)

stephan
  • 356
  • 3
  • 9
  • P.S. I did try to add charts to the question but I don't have enough reputation to do that apparently – stephan Feb 28 '19 at 04:56

1 Answers1

17

As demonstrated briefly in the Bin transforms section of the documentation, you can pass an alt.Bin() instance to fine-tune the binning parameters.

The equivalent of your matplotlib histogram would be something like this:

alt.Chart(my_numbers_df).mark_bar().encode(
    alt.X("Integers", bin=alt.Bin(extent=[-4, 4], step=0.5)),
    y='count()',
)

enter image description here

Martin
  • 659
  • 7
  • 14
jakevdp
  • 77,104
  • 11
  • 125
  • 160
  • That link got moved: here's the updated one if you are looking https://altair-viz.github.io/user_guide/transform/bin.html – mcnutt Feb 11 '21 at 16:26