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.
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)):
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].
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()
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¶
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¶
Documentation: https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html
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
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¶
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¶
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()
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.
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()