03-28-2022, 07:29 AM
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. To get the list, import the lines from the library.
Next, get the keys from the attribute.
You can then print the styles and choose your preferred style per plot.
The result is
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.
Next, we create lists for each group of employees’ salaries.
Now that we have some data to practice various line styles in, let’s go ahead and manipulate the data.
Practical Ways To Use Line Styles In Matplotlib
➥Get the required functions
Install the library using pip.
After installing, create a file and import the library’s functions into it. I have created one called and imported and as follows.
We will get the line styles from lines and plot the lines with. It is a convention to shorten as . 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.
We can plot our employee values as follows.
The 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.
![[Image: image-153.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-153.png)
Now let us label the lines and add a title to the plots for easier identification. Insert the code before the line.
Output:
![[Image: image-154.png]](https://blog.finxter.com/wp-content/uploads/2022/03/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
➤legend represents the area that describes specific elements of the graph. In this case, we will use the method to describe the labels assigned previously.
Output:
![[Image: image-155.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-155.png)
Full code: Here’s the full code that helps to represent our data in the form of different lines.
Looks better, right? Yes, but we can improve the visuals by changing the line styles.
[b]
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 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 method, run the print line below the import statement.
We get a dictionary which represents the type of styles in which the lines can be represented as follows:
Let’s find the keys and store them as.
We get a list on running printing.
Let’s use the result to change line styles for the plots.
Combining the and helps us change the and plots as follows.
Output:
![[Image: image-156.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-156.png)
Here, show the styles, while ‘s attribute changes the lines graphically.
Tidbits
Output:
![[Image: image-157.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-157.png)
Output:
![[Image: image-158.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-158.png)
Output:
![[Image: image-159.png]](https://blog.finxter.com/wp-content/uploads/2022/03/image-159.png)
Conclusion
You can get a list of styles in using the lines attribute. After storing the target styles, you can change the lines’ appearance using ‘s built-in 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!
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:
matplotlibCode:
matplotlibCode:
from matplotlib import linesNext, get the keys from the
Code:
lineStyleCode:
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:
matplotlibPractical Ways To Use Line Styles In Matplotlib
➥Get the required functions
Install the
Code:
matplotlibCode:
pip install matplotlib
# OR
pip3 install matplotlibAfter installing
Code:
MatplotlibCode:
employees.pyCode:
linesCode:
pyplotCode:
from matplotlib import lines, pyplot as pltWe will get the line styles from lines and plot the lines with
Code:
pyplotCode:
pyplotCode:
plt➥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()Code:
python employees.py![[Image: image-153.png]](https://blog.finxter.com/wp-content/uploads/2022/03/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()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]](https://blog.finxter.com/wp-content/uploads/2022/03/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()Code:
plt.legend()
plt.show()Output:
![[Image: image-155.png]](https://blog.finxter.com/wp-content/uploads/2022/03/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]
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:
linesLet’s print the styles from the imported lines. After commenting out the
Code:
plt.show()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_stylesCode:
line_styles = lines.lineStyles.keys()We get a list on running printing
Code:
line_stylesCode:
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()Code:
pyplot.linestyleCode:
designersCode:
developersCode:
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]](https://blog.finxter.com/wp-content/uploads/2022/03/image-156.png)
Here,
Code:
lines.lineStylesCode:
pyplotCode:
linestyle
Tidbits- You can also style the lines using markers, width, and colors. For example, you can mark the ‘ plot usingCode:
accountantsas follows.Code:o
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]](https://blog.finxter.com/wp-content/uploads/2022/03/image-157.png)
- You can increase the width of the plot from 1 to 3 as followsCode:
designers
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]](https://blog.finxter.com/wp-content/uploads/2022/03/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]](https://blog.finxter.com/wp-content/uploads/2022/03/image-159.png)
Conclusion
You can get a list of styles in
Code:
matplotlibCode:
pyplotCode:
linestyleRecommended: Matplotlib — A Simple Guide with Videos
Please stay tuned and subscribe for more interesting discussions in the future. Happy learning!

