Matplotlib Line
In the plotting process, we can customize the line style, including line type, color, size, etc.
### Line Types
Line types can be defined using the **linestyle** parameter, abbreviated as ls.
| Type | Abbreviation | Description |
| --- | --- | --- |
| 'solid' (default) | '-' | Solid line |
| 'dotted' | ':' | Dotted line |
| 'dashed' | '--' | Dashed line |
| 'dashdot' | '-.' | Dash-dot line |
| 'None' | '' or ' ' | No line |
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, linestyle ='dotted')
plt.show()
The result is as follows:
!(#)
Using abbreviation:
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, ls ='-.')
plt.show()
The result is as follows:
!(#)
### Line Colors
Line colors can be defined using the **color** parameter, abbreviated as c.
Color Types:
| Color Marker | Description |
| --- | --- |
| 'r' | Red |
| 'g' | Green |
| 'b' | Blue |
| 'c' | Cyan |
| 'm' | Magenta |
| 'y' | Yellow |
| 'k' | Black |
| 'w' | White |
Of course, you can also customize color types, such as: **SeaGreen, #8FBC8F**, etc. For complete styles, refer to (#).
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, color ='r')
plt.show()
The result is as follows:
!(#)
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, c ='#8FBC8F')
plt.show()
The result is as follows:
!(#)
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, c ='SeaGreen')
plt.show()
The result is as follows:
!(#)
### Line Width
Line width can be defined using the **linewidth** parameter, abbreviated as lw. The value can be a floating point number, such as: **1**, **2.0**, **5.67**, etc.
## Example
import matplotlib.pyplot as plt
import numpy as np
ypoints = np.array([6,2,13,10])
plt.plot(ypoints, linewidth ='12.5')
plt.show()
The result is as follows:
!(#)
### Multiple Lines
The plot() method can include multiple pairs of x,y values to draw multiple lines.
## Example
import matplotlib.pyplot as plt
import numpy as np
y1 = np.array([3,7,5,9])
y2 = np.array([6,2,13,10])
plt.plot(y1)
plt.plot(y2)
plt.show()
As you can see from the figure above, the **x** values are set to **[0, 1, 2, 3]** by default.
The result is as follows:
We can also set our own x coordinate values:
!(#)
## Example
import matplotlib.pyplot as plt
import numpy as np
x1 = np.array([0,1,2,3])
y1 = np.array([3,7,5,9])
x2 = np.array([0,1,2,3])
y2 = np.array([6,2,13,10])
plt.plot(x1, y1, x2, y2)
plt.show()
The result is as follows:
!(#)
YouTip