Sick Gaming
[Tut] Is There A List Of Line Styles In Matplotlib? - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Is There A List Of Line Styles In Matplotlib? (/thread-99083.html)



[Tut] Is There A List Of Line Styles In Matplotlib? - xSicKxBot - 03-28-2022

Is There A List Of Line Styles In Matplotlib?

<div><p>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.</p>
<p>Yes, there is a list of line styles in <code><a href="https://blog.finxter.com/matplotlib-full-guide/" target="_blank" rel="noreferrer noopener">matplotlib</a></code>. To get the list, import the lines from the <code>matplotlib</code> library.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from matplotlib import lines</pre>
<p>Next, get the keys from the <code>lineStyle</code> attribute.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lines.lineStyles.keys()</pre>
<p>You can then print the styles and choose your preferred style per plot.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(lines.lineStyles.keys())</pre>
<p>The result is</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])</pre>
<p>As you will see in this tutorial, you can also generate line styles using colors and markers. What is more? Find out below.</p>
<h2>Lab Setup To explore Line Styles In <strong><a href="https://blog.finxter.com/matplotlib-full-guide/" target="_blank" rel="noreferrer noopener">Matplotlib</a></strong></h2>
<p>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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">ages = [20, 21, 22, 23, 24, 25, 26, 27, 28, 29]</pre>
<p>Next, we create lists for each group of employees’ salaries.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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]</pre>
<p>Now that we have some data to practice various line styles in <code>matplotlib</code>, let’s go ahead and manipulate the data.</p>
<h2>Practical Ways To Use Line Styles In Matplotlib</h2>
<h3>➥Get the required functions</h3>
<p>Install the <code>matplotlib</code> library using pip.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">pip install matplotlib
# OR
pip3 install matplotlib</pre>
<p>After installing <code>Matplotlib</code>, create a file and import the library’s functions into it. I have created one called <code>employees.py</code> and imported <code>lines</code> and <code>pyplot</code> as follows.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">from matplotlib import lines, pyplot as plt</pre>
<p>We will get the line styles from lines and plot the lines with <code>pyplot</code>. It is a convention to shorten <code>pyplot</code> as <code>plt</code>. In simple words it is an alias.</p>
<h3>➥Plot the data</h3>
<p>Plotting a bunch of data on a curve requires specifying the values for the x-axis and y-axis.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.plot(&lt;x-axis data list&gt;, &lt;y-axis data list&gt;)</pre>
<p>We can plot our employee values as follows.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p>The <code>plt.show()</code> 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.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">python employees.py</pre>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="601" height="446" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-153.png" alt="List of Line Styles in Python - Figure 1" class="wp-image-250426" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-153.png 601w, https://blog.finxter.com/wp-content/uploads/2022/03/image-153-300x223.png 300w" sizes="(max-width: 601px) 100vw, 601px" /></figure>
<p>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> line.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.xlabel("Ages")
plt.ylabel("Annual salary in USD")
plt.title("Salaries of X employees for 20-29 ages")</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="627" height="462" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-154.png" alt="List of Line Styles in Python - Figure 2" class="wp-image-250428" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-154.png 627w, https://blog.finxter.com/wp-content/uploads/2022/03/image-154-300x221.png 300w" sizes="(max-width: 627px) 100vw, 627px" /></figure>
<p>However, we cannot tell what the green, blue, and coral lines represent. So, let’s mark the lines using labels and a legend.</p>
<p>➤<em>labels</em></p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.plot(ages, designers, label='designers')
plt.plot(ages, developers, label='developers')
plt.plot(ages, accountants, label='accountants')</pre>
<p>➤<em>legend</em> represents the area that describes specific elements of the graph. In this case, we will use the <code>legend()</code> method to describe the labels assigned previously.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="1" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">plt.legend()
plt.show()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="627" height="466" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-155.png" alt="Displaying Legends in Graph" class="wp-image-250452" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-155.png 627w, https://blog.finxter.com/wp-content/uploads/2022/03/image-155-300x223.png 300w" sizes="(max-width: 627px) 100vw, 627px" /></figure>
<p><strong>Full code:</strong> Here’s the full code that helps to represent our data in the form of different lines. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p>Looks better, right? Yes, but we can improve the visuals by changing the line styles.</p>
<h2><strong><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" />Change The Line Styles</strong></h2>
<p>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</code> module. You can import lines from matplotlib as we have done above or get them from <a href="https://matplotlib.org/1.5.3/api/pyplot_api.html#matplotlib.pyplot.plot">Matplotlib line style docs</a>.</p>
<p>Let’s print the styles from the imported lines. After commenting out the <code>plt.show()</code> method, run the print line below the import statement.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(lines.lineStyles)</pre>
<p>We get a <a href="https://blog.finxter.com/python-dictionary/" target="_blank" rel="noreferrer noopener">dictionary</a> which represents the type of styles in which the lines can be represented as follows:</p>
<pre class="wp-block-code"><code>{'-': '_draw_solid', '--': '_draw_dashed', '-.': '_draw_dash_dot', ':': '_draw_dotted', 'None': '_draw_nothing', ' ': '_draw_nothing', '': '_draw_nothing'}</code></pre>
<p>Let’s find the keys and store them as <code>list_styles</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">line_styles = lines.lineStyles.keys()</pre>
<p>We get a list on running printing <code>line_styles</code>.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(line_styles)</pre>
<pre class="wp-block-code"><code>dict_keys(['-', '--', '-.', ':', 'None', ' ', ''])</code></pre>
<p>Let’s use the result to change line styles for the plots.</p>
<p>Combining the <code>lines.lineStyles.keys()</code> and <code>pyplot.linestyle</code> helps us change the <code>designers</code> and <code>developers</code> plots as follows.</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="9,10" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="597" height="433" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-156.png" alt="" class="wp-image-250474" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-156.png 597w, https://blog.finxter.com/wp-content/uploads/2022/03/image-156-300x218.png 300w" sizes="(max-width: 597px) 100vw, 597px" /></figure>
<p>Here, <code>lines.lineStyles</code> show the styles, while <code>pyplot</code>‘s <code>linestyle</code> attribute changes the lines graphically.</p>
<p><strong><img src="https://s.w.org/images/core/emoji/13.1.0/72x72/1f60e.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /></strong> <strong>Tidbits</strong></p>
<ul>
<li>You can also style the lines using markers, width, and colors. For example, you can mark the <code>accountants</code>‘ plot using <code>o</code> as follows.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="590" height="437" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-157.png" alt="" class="wp-image-250662" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-157.png 590w, https://blog.finxter.com/wp-content/uploads/2022/03/image-157-300x222.png 300w" sizes="(max-width: 590px) 100vw, 590px" /></figure>
<ul>
<li>You can increase the width of the <code>designers</code> plot from 1 to 3 as follows</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="593" height="435" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-158.png" alt="" class="wp-image-250663" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-158.png 593w, https://blog.finxter.com/wp-content/uploads/2022/03/image-158-300x220.png 300w" sizes="(max-width: 593px) 100vw, 593px" /></figure>
<ul>
<li>Lastly you can customize the plot colors using the color aliases, hex colors or names.</li>
</ul>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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()</pre>
<p><strong>Output:</strong></p>
<figure class="wp-block-image size-full is-style-default"><img loading="lazy" width="605" height="429" src="https://blog.finxter.com/wp-content/uploads/2022/03/image-159.png" alt="" class="wp-image-250665" srcset="https://blog.finxter.com/wp-content/uploads/2022/03/image-159.png 605w, https://blog.finxter.com/wp-content/uploads/2022/03/image-159-300x213.png 300w" sizes="(max-width: 605px) 100vw, 605px" /></figure>
<h2>Conclusion</h2>
<p>You can get a list of styles in <code>matplotlib</code> using the lines attribute. After storing the target styles, you can change the lines’ appearance using <code>pyplot</code>‘s built-in <code>linestyle</code> attribute. Additionally, as illustrated in this tutorial, you can differentiate lines using markers, width, and colors.</p>
<p class="has-text-align-center has-base-background-color has-background"><strong>Recommended: <a href="https://blog.finxter.com/matplotlib-full-guide/" target="_blank" rel="noreferrer noopener">Matplotlib — A Simple Guide with Videos</a></strong></p>
<p>Please <strong><a href="https://www.youtube.com/channel/UCRlWL2q80BnI4sA5ISrz9uw">stay tuned</a></strong> and <strong><a href="https://blog.finxter.com/subscribe" target="_blank" rel="noreferrer noopener">subscribe</a></strong> for more interesting discussions in the future. Happy learning!</p>
</div>


https://www.sickgaming.net/blog/2022/03/19/is-there-a-list-of-line-styles-in-matplotlib/