<jupyter_start><jupyter_text>Matplotlib linestyle standard examplesIn this notebook I will generate a plot with all available linestyle options to draw a line plot with matplotlib.The final figure is this one:![matplotlib_linestyles.png](attachment:matplotlib_linestyles.png) Then I show the step by step used to obtain the above graph.---  ImportsLet's start by importing sugar, spice and everything nice:- *matplotlib* to draw the graph;- *numpy* to generate some data;<jupyter_code>import matplotlib.pyplot as plt
import numpy as np<jupyter_output><empty_output><jupyter_text>Checking versions Python```pythonimport platformprint('Python version: ' + platform.python_version())```Python version: 3.7.3 IPython```pythonimport IPythonprint('IPython version: ' + IPython.__version__)```IPython version: 7.22.0 matplotlib```pythonimport matplotlib as mplprint('matplotlib version: ' + mpl.__version__)```matplotlib version: 3.3.4 NumPy```pythonimport numpy as npprint('NumPy version: ' + np.__version__)```NumPy version: 1.19.2  Acessing all linestyles availableBefore generating th graph, we need to obtain the linestyles from the matplotlib.One way to obtain them is through the ```lines``` class ([documentation](https://matplotlib.org/stable/api/lines_api.html)). First, we import this class:<jupyter_code>from matplotlib import lines<jupyter_output><empty_output><jupyter_text>And then assign the linestyles to a variable:<jupyter_code>lines_options = lines.lineStyles
lines_options<jupyter_output><empty_output><jupyter_text>The linestyles are stored in a dictionary, where the keys contains the representations of the lines, and the values contains a simple description of the respective linestyle.Now, I'm going to create two lists, one with the symbol of the linestyles (keys) and another with the name of the linestyles (values):<jupyter_code>line_styles = list(lines_options.keys())
line_description = list(lines_options.values())<jupyter_output><empty_output><jupyter_text>And I'm going to create another list with both description and symbol, which will be used in the plot legend. However, the underline at the beginning of the name will cause problems when generating subtitles, raising the following error:```No handles with labels found to put in legend.```To work around this issue, I will add a blank space before the underline.<jupyter_code>lines_description = []
for i in range(len(line_styles)):
    lines_description.append(' ' + line_description[i] + ' ( "' + line_styles[i] + '" ) ')
lines_description[0]<jupyter_output><empty_output><jupyter_text>Generating the dataSince I only need lines to demonstrate the style of the lines, I will create a variable for the *x* axis with only two points, and another variable for the *y* axis also with two points, but with only numbers 1:<jupyter_code>x = np.arange(1,3)
y = np.ones(2)<jupyter_output><empty_output><jupyter_text>Generating the plotTo plot th graph, I will create a figure:    ```python    plt.figure(figsize=(12,6))```Then I will add each linestyle with a ```plt.plot()``` in a for loop, which will iterate through all itens in lines_description:```python    for i in range(len(lines_description)):    plt.plot(x,              y+i,              linewidth=2,              linestyle=line_styles[i],              label=lines_description[i])```The ```x``` is the values for the *x* axis;The ```y + i ``` is the values for the *y* axis. In each iteration, the value of y is increased by the value of i, which prevents the lines from overlapping and leaves a homogeneous spacing between the lines;The ```linewidth = 2``` changes the line width to 2;The ```linestyle=line_styles[i]``` changes the line style based on the ```line_style``` *list*;The ```label=lines_description[i]``` changes the label for each line based on the ```lines_description``` *list*;After this, I will change the range of the *y* axis to improve readability:```python    plt.ylim(0,i+1)    ```The, I will remove the ticks of the *x* and *y* axis:```python    plt.xticks([])  removendo os ticks do eixo xplt.yticks([])  removendo os ticks do eixo x```And I Will also add a title:```python    plt.title("Matplotlib linestyle standard options")```Then I will add the legend outside the figure:```python    plt.legend(bbox_to_anchor=(1,.8)) ```The ```bbox_to_anchor=(1,.8)``` changes the legend position;Then I save the plot:```python    plt.savefig("matplotlib_linestyles.png", dpi=100, bbox_inches='tight')```The ```dpi``` sets the pixel density;The ```bbox_inches='tight'``` makes the legend not to be cut off in the saved version of the graph.Finally, I show the plot:```python    plt.show()```<jupyter_code>plt.figure(figsize=(12,6))
for i in range(len(lines_description)):
    plt.plot(x, 
             y + i, 
             linewidth = 2, 
             linestyle = line_styles[i], 
             label=lines_description[i])
plt.ylim(0,i+1)    
plt.xticks([]) 
plt.yticks([]) 
plt.title("Matplotlib linestyle standard options")
plt.legend(bbox_to_anchor=(1,.8))    
plt.savefig("matplotlib_linestyles.png", dpi=100, bbox_inches='tight')
plt.show()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup