Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 14.9 — Charts with Matplotlib
Python · Example 1 of 1

The counts behind a histogram

Change bins=5 to bins=10 and watch the bars split. The shape of a histogram depends on the bin choice.

Matplotlib draws charts from NumPy and pandas data: line, bar, scatter and histogram, with titles and labels, saved as a PNG file. A chart answers questions that a table of numbers hides.

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Write the code (not run) for a bar chart of three subjects and their marks, with a title and axis labels, saved as marks.png.

02

Use np.histogram to count how many of [1, 2, 2, 3, 3, 3, 4] fall into 3 bins.

03

Say which chart you would use to show loss over 20 training epochs.

Explain it without notes

01

What is the difference between a figure and an axes?

02

Which chart would you use for a trend, a comparison, a relationship and a distribution?

03

What does a histogram's bin count do to the picture?

04

Why do the plotting examples in this topic not run in the browser, and how do you run them?

The counts behind a histogram
Sign in to run this example in your browser.

Expected output

counts: [18, 122, 243, 106, 11]
edges : [19.4, 32.2, 44.9, 57.7, 70.5, 83.2]
total : 500
  19.4 | #
  32.2 | ############
  44.9 | ########################
  57.7 | ##########
  70.5 | #