Defining plot line styles, properties, and format strings

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.

Getting ready

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.

How to do it...

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.

How it works...

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:

Property

Value type

Description

alpha

float

Sets the alpha value used for blending; not supported on all backends.

color or c

Any matplotlib color

Sets the color of the line.

dashes

Sequence of on/off ink in points

Sets the dash sequence, the sequence of dashes with on/off ink in points. If seq is empty or if seq = (None, None), linestyle will be set to solid.

label

Any string

Sets the label to s for auto legend.

linestyle or ls

[ '-' | '–' | '-.' | ':' | 'steps' | ...]

Sets the linestyle of the line (also accepts drawstyles).

linewidth or lw

float value in points

Sets the line width in points.

marker

[ 7 | 4 | 5 | 6 | 'o' | 'D' | 'h' | 'H' | '_' | '' | 'None' | ' ' | None | '8' | 'p' | ',' | '+' | '.' | 's' | '*' | 'd' | 3 | 0 | 1 | 2 | '1' | '3' | '4' | '2' | 'v' | '<' | '>' | '^' | '|' | 'x' | '$...$' | tuple | Nx2 array ]

Sets the line marker.

markeredgecolor or mec

Any matplotlib color

Sets the marker edge color.

markeredgewidth or mew

float value in points

Sets the marker edge width in points.

markerfacecolor or mfc

Any matplotlib color

Set the marker face color.

markersize or ms

float

Set the marker size in points.

solid_capstyle

['butt' | 'round' | 'projecting']

Set the cap style for solid line styles.

solid_joinstyle

['miter' | 'round' | 'bevel']

Set the join style for solid line styles.

visible

[True | False]

Set the artist's visibility.

xdata

np.array

Set the data np.array for x.

ydata

np.array

Set the data np.array for y.

Zorder

Any number

Set the z axis order for the artist. Artists with lower Zorder values are drawn first.

If x and y are axes going horizontal to the right and vertical to the top of the screen, the z axis is the one extending toward the viewer. So 0 value would be at the screen, 1, one layer above, and so on.

The following table shows some linestyles:

Linestyle

Description

'-'

Solid

'--'

Dashed

'-.'

Dash_dot

':'

Dotted

'None', ' ', ''

Draw nothing

The following table shows line markers:

Marker

Description

'o'

Circle

'D'

Diamond

'h'

Hexagon1

'H'

Hexagon2

'_'

Horizontal line

'', 'None', ' ', None

Nothing

'8'

Octagon

'p'

Pentagon

','

Pixel

'+'

Plus

'.'

Point

's'

Square

'*'

Star

'd'

Thin_diamond

'v'

Triangle_down

'<'

Triangle_left

'>'

Triangle_right

'^'

Triangle_up

'|'

Vertical line

'x'

X

Color

We can get all colors that matplotlib supports by calling matplotlib.pyplot.colors(); this will give the following results:

Alias

Color

B

Blue

G

Green

R

Red

C

Cyan

M

Magenta

Y

Yellow

K

Black

W

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')

Background color

By providing axisbg to a function such as matplotlib.pyplot.axes() or matplotlib.pyplot.subplot(), we can define the background color of an axis as shown here:

subplot(111, axisbg=(0.1843, 0.3098, 0.3098))
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.242.157