# 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 `matplotlib`. To get the list, import the lines from the `matplotlib` library.

`from matplotlib import lines`

Next, get the keys from the `lineStyle` attribute.

`lines.lineStyles.keys()`

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

`print(lines.lineStyles.keys())`

The result is

`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 Matplotlib

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.

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

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

```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 `matplotlib`, let’s go ahead and manipulate the data.

## Practical Ways To Use Line Styles In Matplotlib

### β₯Get the required functions

Install the `matplotlib` library using pip.

```pip install matplotlib
# OR
pip3 install matplotlib```

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

`from matplotlib import lines, pyplot as plt`

We will get the line styles from lines and plot the lines with `pyplot`. It is a convention to shorten `pyplot` as `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.

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

We can plot our employee values as follows.

```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 `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.

`python employees.py`

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

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

Output:

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

β€labels

```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 `legend()` method to describe the labels assigned previously.

```plt.legend()
plt.show()```

Output:

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

```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.

## π‘Change The Line Styles

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 `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 `plt.show()` method, run the print line below the import statement.

`print(lines.lineStyles)`

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

``{'-': '_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 `list_styles`.

`line_styles = lines.lineStyles.keys()`

We get a list on running printing `line_styles`.

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

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

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

```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:

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

π Tidbits

• You can also style the lines using markers, width, and colors. For example, you can mark the `accountants`‘ plot using `o` as follows.
```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:

• You can increase the width of the `designers` plot from 1 to 3 as follows
```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:

• Lastly you can customize the plot colors using the color aliases, hex colors or names.
```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:

## Conclusion

You can get a list of styles in `matplotlib` using the lines attribute. After storing the target styles, you can change the lines’ appearance using `pyplot`‘s built-in `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!