This recipe shows how we can change various line properties such as styles, colors, or width. Having lines set up appropriately according to the information presented and distinct enough for target audiences (if the audience is a younger population, we may want to target them with more vivid colors; if they are older, we may want to use more contrasting colors) can make the difference between being barely noticeable and leaving a great impact on the viewer.
Although we stressed how important it is to aesthetically tune your presentation, we first must learn how to do it.
If you don't have a particular eye for color matching, there are free and commercial online tools that can generate color sets for you. One of the most well known is Colorbrewer2, which can be found at http://colorbrewer2.org/.
Some serious research has been conducted on the usage of color in data visualizations, but explaining that theory is out of the scope of this book. The material on the topic is a must read if you are working with more advanced visualizations daily.
Let's learn how to change line properties. We can change the lines in our plots using different methods and approaches.
The first and most common method is to define lines by passing keyword parameters to functions such as plot()
:
plot(x, y, linewidth=1.5)
Because a call to plot()
returns the line instance (matplotlib.lines.Line2D
), we can use a set of setter methods on that instance to set various properties:
line, = plot(x, y) line.set_linewidth(1.5)
Those who used MATLABÒ will feel the need to use a third way of configuring line properties using the setp()
function:
lines = plot(x, y) setp(lines, 'linewidth', 1.5)
Another way to use setp()
is this:
setp(lines, linewidth=1.5)
Whatever way you prefer to configure lines, choose one method and stay consistent for the whole project (or at least a file). This way, when you (or someone else in the future) come back to the code, it will be easier to make sense of it and change it.
All the properties we can change for a line are contained in the matplotlib.lines.Line2D
class. We list some of them in the following table:
The following table shows some linestyles:
Linestyle |
Description |
---|---|
|
Solid |
|
Dashed |
|
Dash_dot |
|
Dotted |
|
Draw nothing |
The following table shows line markers:
Marker |
Description |
---|---|
|
Circle |
|
Diamond |
|
Hexagon1 |
|
Hexagon2 |
|
Horizontal line |
|
Nothing |
|
Octagon |
|
Pentagon |
|
Pixel |
|
Plus |
|
Point |
|
Square |
|
Star |
|
Thin_diamond |
|
Triangle_down |
|
Triangle_left |
|
Triangle_right |
|
Triangle_up |
|
Vertical line |
|
X |
We can get all colors that matplotlib supports by calling matplotlib.pyplot.colors()
; this will give the following results:
Alias |
Color |
---|---|
|
Blue |
|
Green |
|
Red |
|
Cyan |
|
Magenta |
|
Yellow |
|
Black |
|
White |
These colors can be used in different matplotlib functions that take color arguments.
If these basic colors are not enough and as we progress, they will not be enough.We can use two other ways of defining a color value. We can use an HTML hexadecimal string as shown here:
color = '#eeefff'
We can also use legal HTML color names ('red'
, 'chartreuse'
). We can also pass an RGB tuple normalized to [0, 1]
:
color = (0.3, 0.3, 0.4)
The argument color is accepted by a range of functions such as title()
:
title('Title in a custom color', color='#123456')