Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Is There A List Of Line Styles In Matplotlib?
1
Is There A List Of Line Styles In Matplotlib?

You want to plot a series of data with unique styles. You need to pick various line styles from a list but not sure how to get started. This tutorial will help you out.

Yes, there is a list of line styles in
Code:
matplotlib
. To get the list, import the lines from the
Code:
matplotlib
library.

Code:
from matplotlib import lines

Next, get the keys from the
Code:
lineStyle
attribute.

Code:
lines.lineStyles.keys()

You can then print the styles and choose your preferred style per plot.

Code:
print(lines.lineStyles.keys())

The result is

Code:
dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])

As you will see in this tutorial, you can also generate line styles using colors and markers. What is more? Find out below.

Lab Setup To explore Line Styles In [b]Matplotlib[/b]

Assume we want to plot the annual earnings of company X’s employees (designers, developers, and accountants) aged between 20 and 29. We store the ages in a list as follows.

Code:
ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

Next, we create lists for each group of employees’ salaries.

Code:
designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000]

Now that we have some data to practice various line styles in
Code:
matplotlib
, let’s go ahead and manipulate the data.

Practical Ways To Use Line Styles In Matplotlib

➥Get the required functions

Install the
Code:
matplotlib
library using pip.

Code:
pip install matplotlib # OR pip3 install matplotlib

After installing
Code:
Matplotlib
, create a file and import the library’s functions into it. I have created one called
Code:
employees.py
and imported
Code:
lines
and
Code:
pyplot
as follows.

Code:
from matplotlib import lines, pyplot as plt

We will get the line styles from lines and plot the lines with
Code:
pyplot
. It is a convention to shorten
Code:
pyplot
as
Code:
plt
. In simple words it is an alias.

➥Plot the data

Plotting a bunch of data on a curve requires specifying the values for the x-axis and y-axis.

Code:
plt.plot(<x-axis data list>, <y-axis data list>)

We can plot our employee values as follows.

Code:
ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers) plt.plot(ages, developers) plt.plot(ages, accountants) plt.show()

The
Code:
plt.show()
function instructs Python to reveal the graphical information cached in the memory. As soon as you run the file on the terminal, you get three solid lines of different colors.

Code:
python employees.py
[Image: image-153.png]

Now let us label the lines and add a title to the plots for easier identification. Insert the code before the
Code:
plt.show()
line.

Code:
plt.xlabel("Ages") plt.ylabel("Annual salary in USD") plt.title("Salaries of X employees for 20-29 ages")

Output:

[Image: image-154.png]

However, we cannot tell what the green, blue, and coral lines represent. So, let’s mark the lines using labels and a legend.

➤labels

Code:
plt.plot(ages, designers, label='designers') plt.plot(ages, developers, label='developers') plt.plot(ages, accountants, label='accountants')

➤legend represents the area that describes specific elements of the graph. In this case, we will use the
Code:
legend()
method to describe the labels assigned previously.

Code:
plt.legend() plt.show()

Output:

[Image: image-155.png]

Full code: Here’s the full code that helps to represent our data in the form of different lines.

Code:
from matplotlib import lines, pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers, label='designers') plt.plot(ages, developers, label='developers') plt.plot(ages, accountants, label='accountants') plt.xlabel("Ages") plt.ylabel("Annual salary in USD") plt.title("Salaries of X employees for 20-29 ages") plt.legend() plt.show()

Looks better, right? Yes, but we can improve the visuals by changing the line styles.

[b][Image: 1f4a1.png]Change The Line Styles[/b]

We assigned different labels to distinguish different data but is there a way to change the style of the line? Yes! The first step is to grab the lines with the help of the
Code:
lines
module. You can import lines from matplotlib as we have done above or get them from Matplotlib line style docs.

Let’s print the styles from the imported lines. After commenting out the
Code:
plt.show()
method, run the print line below the import statement.

Code:
print(lines.lineStyles)

We get a dictionary which represents the type of styles in which the lines can be represented as follows:

Code:
{'-': '_draw_solid', '--': '_draw_dashed', '-.': '_draw_dash_dot', ':': '_draw_dotted', 'None': '_draw_nothing', ' ': '_draw_nothing', '': '_draw_nothing'}

Let’s find the keys and store them as
Code:
list_styles
.

Code:
line_styles = lines.lineStyles.keys()

We get a list on running printing
Code:
line_styles
.

Code:
print(line_styles)
Code:
dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])

Let’s use the result to change line styles for the plots.

Combining the
Code:
lines.lineStyles.keys()
and
Code:
pyplot.linestyle
helps us change the
Code:
designers
and
Code:
developers
plots as follows.

Code:
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers, label='designers', linestyle='--') plt.plot(ages, developers, label='developers', linestyle=':') plt.show()

Output:

[Image: image-156.png]

Here,
Code:
lines.lineStyles
show the styles, while
Code:
pyplot
‘s
Code:
linestyle
attribute changes the lines graphically.

[Image: 1f60e.png] Tidbits
  • You can also style the lines using markers, width, and colors. For example, you can mark the
    Code:
    accountants
    ‘ plot using
    Code:
    o
    as follows.

Code:
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, accountants, label='accountants', marker='o') plt.show()

Output:

[Image: image-157.png]
  • You can increase the width of the
    Code:
    designers
    plot from 1 to 3 as follows

Code:
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] plt.plot(ages, designers, label='designers', linestyle='-.', linewidth=3) plt.show()

Output:

[Image: image-158.png]
  • Lastly you can customize the plot colors using the color aliases, hex colors or names.

Code:
from matplotlib import pyplot as plt ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29] designers = [45893, 50375, 52000, 52009, 54000, 55555, 56979, 57040, 55991, 54000] developers = [55893, 53375, 52000, 51008, 58000, 58555, 59979, 60050, 65991, 67000] accountants = [40000, 41500, 42000, 42508, 43001, 44555, 46979, 48050, 49991, 51000] plt.plot(ages, designers, label='designers', linestyle='-.', color='b') plt.plot(ages, developers, label='developers', linestyle=':', linewidth=3, color='#6dee6d') plt.plot(ages, accountants, label='accountants', marker='o', color='gold') plt.show()

Output:

[Image: image-159.png]
Conclusion

You can get a list of styles in
Code:
matplotlib
using the lines attribute. After storing the target styles, you can change the lines’ appearance using
Code:
pyplot
‘s built-in
Code:
linestyle
attribute. Additionally, as illustrated in this tutorial, you can differentiate lines using markers, width, and colors.

Recommended: Matplotlib — A Simple Guide with Videos

Please stay tuned and subscribe for more interesting discussions in the future. Happy learning!
Reply

Users browsing this thread:
1 Guest(s)


Forum Jump: