Breaking News

# Line Plot using Matplotlib

2. Line Plot

### Plot a Line Plot Using Matplotlib¶

To plot a line plot in Matplotlib, you use the generic plot() function from the PyPlot instance. There’s no specific lineplot() function – the generic one automatically plots using lines or markers.

In [2]:
import matplotlib.pyplot as plt
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y)
plt.show()


Alternatively, we could’ve completely omitted the x axis, and just plotted y. This would result in the X-axis being filled with range(len(y)):

In [3]:
import matplotlib.pyplot as plt
Y = [36,19,30,52,25]
plt.plot(Y)
plt.show()


You may be wondering why the x-axis ranges from 0-4 and the y-axis from 0-50. If you provide a single list or array to the plot() command, matplotlib assumes it is a sequence of y values, and automatically generates the x values for you. Since python ranges start with 0, the default x vector has the same length as y but starts with 0. Hence the x data are [0,1,2,3,4].

In [4]:
import matplotlib.pyplot as plt
plt.title('Info')
plt.ylabel('Y axis')
plt.xlabel('X axis')
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y)
plt.show()


### Change Font Style and Color¶

#### Colors¶

The following color abbreviations are supported: character color

• ‘b’ blue
• ‘g’ green
• ‘r’ red
• ‘c’ cyan
• ‘m’ magenta
• ‘y’ yellow
• ‘k’ black
• ‘w’ white
In [5]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y)
plt.show()


### Line Width¶

In [6]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y,color='k',linewidth=3)
plt.show()


### Matplotlib Line Style¶

In [7]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y,color='k',linewidth=3,linestyle='dashed')
plt.show()


### Matplotlib Marker¶

• Market Style
• Marker Color
• Marker Size

Documentation: https://matplotlib.org/stable/api/markers_api.html

In [8]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y = [36,19,30,52,25]
plt.plot(X,Y,color='k',linewidth=3,linestyle='solid',marker='o',markerfacecolor='red',markersize=10)
plt.show()


### Multiline Plot¶

In [9]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y1 = [36,19,30,52,25]
Y2 = [6,49,10,72,45]
plt.plot(X,Y1,color='k',linewidth=3,linestyle='solid',marker='o',markerfacecolor='red',markersize=10)
plt.plot(X,Y2,color='blue',linewidth=3,linestyle='solid',marker='o',markerfacecolor='black',markersize=10)
plt.show()


### Legend Plot using Lables¶

In [10]:
import matplotlib.pyplot as plt
plt.title('Info',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.ylabel('Y axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
plt.xlabel('X axis',fontsize=25,fontname='Times New Roman',color='blue',weight='bold')
X = [2,4,6,8,10]
Y1 = [36,19,30,52,25]
Y2 = [6,49,10,72,45]
plt.plot(X,Y1,color='k',linewidth=3,label='Chandigarh')
plt.plot(X,Y2,color='blue',linewidth=3,label='Delhi')
plt.legend()
plt.show()

In [11]:
import matplotlib.pyplot as plt
x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
y = [38496, 42000, 46752, 49320, 53200,56000, 62316, 64928, 67317, 68748, 73752]
plt.plot(x,y, linewidth=3, label='Python')
plt.xlabel("Ages")
plt.ylabel("Median Salary (USD)")
plt.title("Median Salary (USD) by Age")
plt.legend()
plt.grid(True)
plt.tight_layout()


tight_layout automatically adjusts subplot params so that the subplot(s) fits in to the figure area. This is an experimental feature and may not work for some cases. It only checks the extents of ticklabels, axis labels, and titles.

### Using xkcd format¶

Turn on xkcd sketch-style drawing mode. This will only have effect on things drawn after this function is called.

In [12]:
import matplotlib.pyplot as plt
x = [25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]
y = [38496, 42000, 46752, 49320, 53200,56000, 62316, 64928, 67317, 68748, 73752]
plt.xkcd()
plt.plot(x,y, linewidth=3, label='Python')
plt.xlabel("Ages")
plt.ylabel("Median Salary (USD)")
plt.title("Median Salary (USD) by Age")
plt.legend()
plt.grid(True)
plt.tight_layout()