As we saw in the last exercise, each graph in matplotlib is made using the specific graph function for that graph. Inside the parentheses of the function, we put arguments that are specific to each kind of chart: for example, what color and weight to make each line of a line chart, which x and y values to use in a scatterplot or bar chart, or how big to make the bins in a histogram.
Almost all the other changes we can make are done outside of the graph function.
In addition to graph functions, matplotlib also has general functions. These functions allow us to change general aspects of the graph, like its title, legend, or axis scaling and labels. We’re calling these general functions because they apply to all kinds of charts: for example, a scatterplot title is not different from a bar chart title. Each of them would be added to a graph using plt.title()
after the graph is made.
Here are some common general functions:
Add a title | plt.title() |
---|---|
Add a legend | plt.legend() |
Add axis labels | plt.ylabel(), plt.xlabel() |
Adjust axes | plt.xscale(), plt.yticks() |
Print graph | plt.show() |
By stacking graph functions and general functions, we can make and customize many different charts.
Four graph functions and four general functions are represented in the GIF to the right – for now, we’ve blocked out the parameters (what goes inside the parentheses) so that you can focus on comparing the different functions and their outputs.