How to Change Line Properties in Matplotlib (Python)

(1 vote, average 5.00 out of 5)

Matplotlib is a very powerful tool for making graphs in Python.  No graphing utility would be complete without allowing customization of each line on a graph.  As is common with Matplotlib and Python, modifying how your lines look is both easy and powerful. 

 

When using the plot function, it is as simple as passing in a two character string which specifies what kind of line it will be (or if it should just be markers), and what color.  Here is an example of a graph which uses red circles as markers with no line.

1
plot(x_series, y_series, 'ro')
modify_lines_matplotlib_1

The first character in the string represents the color; the second represents the shape of marker or style of line used.  The following tables show a mapping of allowable colors and characters.

character color
‘b’ blue
‘g’ green
‘r’ red
‘c’ cyan
‘m’ magenta
‘y’ yellow
‘k’ black
‘w’ white
character description
'-' solid line style
'--' dashed line style
'-.' dash-dot line style
':' dotted line style
'.' point marker
',' pixel marker
'o' circle marker
'v' triangle_down marker
'^' triangle_up marker
'<' triangle_left marker
'>' triangle_right marker
'1' tri_down marker
'2' tri_up marker
'3' tri_left marker
'4' tri_right marker
's' square marker
'p' pentagon marker
'*' star marker
'h' hexagon1 marker
'H' hexagon2 marker
'+' plus marker
'x' x marker
'D' diamond marker
'd' thin_diamond marker
'|' vline marker
'_' hline marker

Here is another example, this time showing a full python script.

1
2
3
4
5
6
7
8
9
10
11
12
#set up matplotlib and a new figure
import matplotlib.pyplot as plt
plt.figure()
 
#create data
x_series = [0,1,2,3,4,5]
y_series_1 = [x**2 for x in x_series]
y_series_2 = [x**3 for x in x_series]
 
plt.plot(x_series, y_series_1, 'r-')
plt.plot(x_series, y_series_2, 'c--')
plt.savefig("example.png")

modify_lines_matplotlib_2

Partner Links:
Last Updated on Tuesday, 20 July 2010 15:29  
Related Articles

» How to Clean a Game Console Cartridge

A classic symptom of older game systems which use cartridges is that sometimes the game cartridge itself can become corroded and unusable.   There are several ways to clean cartridges, from the easiest to most desperate. First, here are some common things NOT to do in order to clean your game cartridge:  Things NOT To DoDo NOT blow into the cartridge. Yes it is a quick fix. Yes it does work temporarily. However, due to the moisture of you blowing onto the cartridge connectors,...

» How to Track an iPhone or iPad

Since the iPhone and the iPad have an internal GPS receiver, they can be tracked or a lost device can be found using Apple's Mobile Me service.You can learn more about this service or sign up for a free trial of Mobile Me at .

» Using Awk to Print Select Output

Awk is a very powerful utility, but its syntax is less than intuitive to a beginner. Here are a few common but very powerful things you can do with awk:1234567891011121314151617# print line 2 of the fileawk 'NR == 2' myFile.txt # print column 2 of the fileawk '{print $2}' myFile.txt # add up column 2awk '{s += $4} END {print s}' # print all the lines with more than 80 columnsawk 'length($0) > 80' myFile.txt # print the number of lines in the fileawk 'END { print NR }'...