Bar charts can be made with matplotlib. You can create all kinds of variations that change in color, position, orientation and much more. So what’s matplotlib?
Matplotlib is a Python module that lets you plot all kinds of charts. Bar charts is one of the type of charts it can be plot. There are many different variations of bar charts.
Related course: Matplotlib Examples and Video Course
Example
Bar chart
The method bar() creates a bar chart. So how do you use it?
The program below creates a bar chart. We feed it the horizontal and vertical (data) data.
1 | #!/usr/bin/python3 |
Plot color
You can change the color of the bar chart. To do that, just add the color parameter.
The parameter can be set to an English color definition like ‘red’.
Example:
1 | import numpy as np |
Grid lines
If you want grid lines, you can do that. Add the function call .grid() with color, linestyle, width and axis. Optionally you can add an alpha value.
Code like this:
1 | import numpy as np |
Matplotlib labels
Plots need a description. What’s the use of a plot, if the viewer doesn’t know what the numbers represent. Do you want to add labels?
The code below adds labels to a plot.
1 | import numpy as np |
Multiple charts
You can plot multiple bar charts in one plot. Need multiple bar charts?
The code below adds two bar chars by calling the method twice. A width parameter is specified.
1 | import numpy as np |
Stack charts
You can stack bar charts on top of each other. That is particulary useful when you multiple values combine into something greater.
1 | import numpy as np |
If you are new to matplotlib, then I highly recommend this course.