The Python visualization Jupyter notebook tutorial you are looking for

Ronald Leung
4 min readDec 24, 2020

--

Image provided by https://matplotlib.org/_images/anatomy.png

It’s a bright and sunny day. You start up a Jupyter notebook, browse around your data, and then try to build some nice charts. Basic bar, line, yadi yadi yadda, piece of cake! You got an initial chart quickly but wait… a lot of ways to make it better still. Instead of two charts, a multi-line plot is better. Actually multiple subplots would be nice. And the labels’ fonts are obviously too small. The multi series chart needs a better legend. Should this be ploted using Pyplot or Pandas or Seaborn… The endless tweaking continues…

Charting is one of the most common tasks and there are many powerful tools and frameworks. I often time find myself digging for my own notes and code snippets, which has been a tremendous time saver. The goal of this article is to be the one page that contains most of the common tricks I needed for a polished chart. Here are the common scenarios that we’ll walk through, and you can get the source code here:

  1. Basic chart types: Line, dot, pie
  2. Minimum polishing
  3. Pivots and Multi-line charts
  4. Histograms
  5. Subplots
  6. Multiple y-scales

Data Prep

Let’s just get a few sets of data to play with first, and start with something very basic, just two integer arrays. A simple call to .plot() from the pandas dataframe will give you a quick view.

1. Basic chart types

The “kind” parameter in pandas plot allows you to do various common chart types already:

2. Minimum polishing

It’s good to see a line but there’s plenty to be desired in the graph. At the minimum, here is a list of things you must adjust before including chart size, axis labels, titles, and legends. Here’s an example to set these:

3. Pivots and Multi-line charts

Putting two series in the same chart is probably one of the most common request, and it is easy to do with just a few lines of code. In our example, we have two series, column A and B. We will use the values of Column A in varies scales, and values of Column B, plotting them all in one chart with different colors and labels in the Legend. The following snippet does the trick:

4. Tick label ranges and grid

The chart above looks pretty good on first glance, however if there are a lo more values in the X axis, two issues may occur. One issue is that the X axis labels may collide with each other, and the other is that for each y value, the corresponding X label may be hard to align. Rotating the labels and adding a grid can easily solve that.

5. Histogram

Histogram is another very common and useful way to display your data in groups (or bins). Fortunately it’s easy to do. Specifying the bins is not a must, but I find it confusing if you don’t know exactly where the bins are. The following example shows histogram with some minimal polishing necessary.

6. Subplots

One plot is great, but you may get significant issue by glancing multiple plots together. Multi-series work sometimes, but other times you may want to visualize multiple charts side by side but not on the same graphs. Subplots are what you want. The following snippets show how you can specify the number of rows and columns you need, and then use a for loop to plot the charts one by one into the grid.

7. Multiple y scales

When putting two lines into the same chart though, sometimes the values may be in very different scales like this example. One line just look flat when plotted together:

What you may need to do in this case is to use a separate y axis for this column if you want to show it on a separate axis as follows:

That’s all folks! Hope these snippets provided you a quick start. Again the source code is available here.

--

--