![]() |
|
[Tut] Is There A List Of Line Styles In Matplotlib? - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://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? 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![]() 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: ![]() 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: ![]() 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: ![]() Here, Code: lines.lineStylesCode: pyplotCode: linestyle Tidbits
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: ![]()
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: ![]()
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: ![]() 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! |