How to plot two dotted lines and set marker using Matplotlib?
In this article, we will plot two dotted lines and set markers using various functions of the matplotlib package in the python programming language.
We can use the pyplot.plot along with the linestyle parameter function to draw the dotted line.
matplotlib.pyplot.plot(array1,array2,linestyle='dotted')
Note:
- To set the marker, we have to use the label parameter.
- To display the label we have to use the legend method.
Example 1:
In this example, we created four lists(data points) in which two lists. First, we are plotting the first line with two data points with dotted line style by mentioning label and then plotting the second line with two data points with dotted line style by mentioning label and displaying the label by using the legend() function.
Python3
# import matplotlibimport matplotlib.pyplot as plt # create array 1 for first linefirstarray1 = [1, 3, 5, 7, 9, 11, 23, 45, 67, 89] # create array 2 for first linesecondarray1 = [23, 45, 2, 56, 78, 11, 22, 33, 44, 45] # create array 1 for second linefirstarray2 = [2, 4, 6, 8, 10, 11, 22, 33, 44] # create array 2 for second linesecondarray2 = [11, 34, 56, 43, 56, 11, 22, 33, 44] # plot the line1plt.plot(firstarray1, secondarray1, linestyle='dotted', label='line1', linewidth=6, color="pink") # plot the line2plt.plot(firstarray2, secondarray2, linestyle='dotted', label='line2', linewidth=8) plt.legend() # displayplt.show() |
Output:
Example 2:
In this example, we created four lists(data points then plotting the first line with one sin function from the NumPy module with dotted line style by mentioning label and then plotting the second line with cos function from the NumPy module with two data points with dotted line style by mentioning label.
Python3
# import matplotlibimport matplotlib.pyplot as plt # import numpy moduleimport numpy # create array 1 for first linefirstarray1 = [1, 3, 5, 7, 9, 11, 13, 15, 17] # create array 2 for first linesecondarray1 = [23, 45, 2, 56, 78, 45, 67, 23, 11] # create array 1 for second linefirstarray2 = [2, 4, 6, 8, 10, 45, 32, 11, 78] # create array 2 for second linesecondarray2 = [11, 34, 56, 43, 56] # plot the line1 with sin functionplt.plot(firstarray1, numpy.sin(firstarray1), linestyle='dotted', label='line1', linewidth=6, color="green") # plot the line2 with cos functionplt.plot(firstarray2, numpy.cos(secondarray1), linestyle='dotted', label='line2', linewidth=8) plt.legend() # displayplt.show() |
Output:


