Basic Plots
In this section, we are going to go through the different types of simple plots. This includes bar charts, pie charts, stacked bar, and area charts, histograms, box plots, scatter plots and bubble plots. Please refer to the previous chapter to get more details about these plots. More sophisticated plots, such as violin plots, will be covered in the next chapter, using Seaborn instead of Matplotlib.
Bar Chart
The plt.bar(x, height, [width])
creates a vertical bar plot. For horizontal bars, use the plt.barh()
function.
Important parameters:
x
: Specifies the x coordinates of the barsheight
: Specifies the height of the barswidth
(optional): Specifies the width of all bars; the default is 0.8
Example:
plt.bar(['A', 'B', 'C', 'D'], [20, 25, 40, 10])
The preceding code creates a bar plot, as shown in the following diagram:
If you want to...