1. Customizing plots
In this lesson, we're going to go into more detail with Matplotlib and demonstrate how to customize your plots by specifying colors and line styles, adding legends, and customizing ticks and tick labels.
2. Custom colors
When plotting data, you can specify the color of a plot by passing an additional argument to the pyplot plot() function.
The color argument accepts the same color names that the plot() function in MATLAB uses.
3. Custom line styles
Similarly, you can change the style of the line with the linestyle argument. Again, the linestyle argument uses the same symbols for different line styles as MATLAB: a colon for a dotted line, two dashes for a dashed line, and so on.
Unlike MATLAB, in Matplotlib, calling another plotting function will use the current axes to plot it. This is like calling "hold on" in MATLAB. This makes it very easy to overlay plots, each with your own custom color or linestyle to represent your data.
4. Adding a legend
To add a legend to the figure, you first have to pass a label into each plot using the label argument.
Next, call the legend() function in pyplot before you call show().
5. Encoding data in marker color
Sometimes it is useful to vary the color of individual points in a plot based on a third dimension of the data.
In this case, you can pass an array of the same length as x and y to the argument c. Matplotlib will then map the values passed to c onto a colormap and render each point in the corresponding color.
6. Custom tick labels
While matplotlib will automatically select ticks for you, sometimes you need more control.
You can use the xticks() and yticks() functions in pyplot to set xticks and yticks for the current axes. The first argument is the tick locations. Here, we've specified locations for ticks along the y-axis at 1, 2, and 3, and ticks for the x-axis at the integers 0 through 11.
The second, optional argument is a list of the labels themselves.
In this example, 1 in our data represents one million USD, so we've replaced 1 with the string "$1M" to indicate this explicitly in our ticks. Similarly, 0 through 11 on the x-axis represent months of the year, so we've replaced this with a list of the first letter of each month of the year.
7. Let's plot some data!
Now that you've learned a few ways to customize your Matplotlib visualizations let's practice by plotting some data.