Intro to Python

6. Scatter Plots and Bar Plots

Line charts are useful, but they’re not always the best way to plot data. After carefully thinking, you might decide to use a scatter plot, a pie chart, a bar graph, a histogram, or a boxplot instead. In this lesson, you will learn how to create scatter plots and bar plots in Matplotlib.

Scatter Plots

Both scatter plots and line plots showcase points with x and y variables. So, there is not much difference in how to plot them. Instead of using plt.plot(), all you have to do is insert plt.scatter() instead. Parameters for changing colors and sizes are the same. A useful new parameter for scatter plots is alpha, which allows you to change how transparent the points are, with 1 being not transparent at all and 0 being not visible.

Bar Plots

For bar plots, the x-values need to be strings, as bar charts show categorical data. Instead of plt.plot(), plt.bar() is used to plot bar graphs. Note that the default bar graphs are vertical. If you want to plot a horizontal bar chart, you can use plt.barh().

You can change the color of the bars using the color parameter, and the width of the bars using the width parameter. Remember when plotting horizontal bar charts, you need to adjust the height parameter instead of the width.

If you want to add multiple bars for each category, just add another y-value array with the new values and add another plt.bar() function below the first one. A legend can be created using the plt.legend() feature.