bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Python/Data Science and Scientific Python
Python•Data Science and Scientific Python

Matplotlib Line

Linestyle

You can use the keyword argument linestyle , or shorter ls , to change the style of the plotted line:

Example

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linestyle = 'dotted')

plt.show()

Example

plt.plot(ypoints, linestyle = 'dashed')

Shorter Syntax

The line style can be written in a shorter syntax:

linestyle can be written as ls .

dotted can be written as : .

dashed can be written as -- .

Example

plt.plot(ypoints, ls = ':')

Line Styles

You can choose any of these styles:

StyleOr
'solid' (default)'-'
'dotted'':'
'dashed''--'
'dashdot''-.'
'None''' or ' '

Line Color

You can use the keyword argument color or the shorter c to set the color of the line:

Example

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, color = 'r')

plt.show()

You can also use Hexadecimal color values :

Example

...

plt.plot(ypoints, c = '#4CAF50')

...

Or any of the 140 supported color names .

Example

...

plt.plot(ypoints, c = 'hotpink')

...

Line Width

You can use the keyword argument linewidth or the shorter lw to change the width of the line.

The value is a floating number, in points:

Example

import matplotlib.pyplot as plt

import numpy as np

ypoints = np.array([3, 8, 1, 10])

plt.plot(ypoints, linewidth = '20.5')

plt.show()

Multiple Lines

You can plot as many lines as you like by simply adding more plt.plot() functions:

plt.plot()

You can also plot many lines by adding the points for the x- and y-axis for each line in the same plt.plot() function.

(In the examples above we only specified the points on the y-axis, meaning that the points on the x-axis got the the default values (0, 1, 2, 3).)

The x- and y- values come in pairs:

Example

import matplotlib.pyplot as plt

import numpy as np

x1 = np.array([0, 1, 2, 3])

y1 = np.array([3, 8, 1, 10])

x2 = np.array([0, 1, 2, 3])

y2 = np.array([6, 2, 7, 11])

plt.plot(x1, y1, x2, y2)

plt.show()

Previous

Machine Learning - Data Distribution

Next

Machine Learning - Normal Data Distribution

This chapter

Overview
38

Lessons

159m

Read time

1. NumPy Tutorial2. Matplotlib Tutorial3. Machine Learning4. Pandas Tutorial5. Matplotlib Getting Started6. Machine Learning - Mean Median Mode7. Matplotlib Pyplot8. Machine Learning - Standard Deviation9. Matplotlib Plotting10. Machine Learning - Percentiles11. Matplotlib Markers12. Machine Learning - Data Distribution13. Matplotlib Line14. Machine Learning - Normal Data Distribution15. Matplotlib Labels and Title16. Machine Learning - Scatter Plot17. Matplotlib Adding Grid Lines18. Machine Learning - Linear Regression19. Matplotlib Subplot20. Machine Learning - Polynomial Regression21. Matplotlib Scatter22. Machine Learning - Multiple Regression23. Matplotlib Bars24. Machine Learning - Scale25. Matplotlib Histograms26. Machine Learning - Train/Test27. Matplotlib Pie Charts28. Machine Learning - Decision Tree29. Machine Learning - Confusion Matrix30. Machine Learning - Hierarchical Clustering31. Machine Learning - Logistic Regression32. Machine Learning - Grid Search33. Preprocessing - Categorical Data34. Machine Learning - K-means35. Machine Learning - Bootstrap Aggregation (Bagging)36. Machine Learning - Cross Validation37. Machine Learning - AUC - ROC Curve38. Machine Learning - K-nearest neighbors (KNN)

On this page

LinestyleShorter SyntaxLine StylesLine ColorLine WidthMultiple Lines