Line Plot Using Python Matplotlib Library

Python has two popular built-in libraries which serve the purpose of making graphs – matplotlib and seaborn. Using matplotplib you can make any kind of graph with a custom design of your choice. Although for more visually appealing figures you may use seaborn. Dataframe objects also have an associated plot function which you may utilize for most of your visualization requirements. Below python program explains How to build a simple line plot:

# import All the libraries that you may need
import pandas as pd
import numpy as np
from pandas import DataFrame as df
import matplotlib.pyplot as plt
from matplotlib import rcParams
x=np.arange(0,2.05,0.05)
y1=np.sin(np.pi*x)
y2=np.cos(np.pi*x)
rcParams['figure.facecolor']="white"
rcParams['figure.figsize']=8,6
plt.grid(True)
plt.plot(x,y1,color='#003300', linestyle=':',linewidth=2, markersize=6, marker='o',markerfacecolor='yellow', label='sine curve')
plt.plot(x,y2,color='#003333', linestyle='-',linewidth=2, markersize=6, marker='d',markerfacecolor='#e6ffff',markeredgecolor='#006666', label='cosine curve')
plt.xlabel('Angle in Fractions of Pi')
plt.ylabel('Sin(x) , Cos(x)')
plt.title('Sine and Cosine Curve')
plt.legend()
plt.show()

Here we are using arrays. You can also use dataframe or series objects. Before plotting any data you might want set certain properties of the figure that will be generated. You can use rcParams to set the size and background color of the figure. You can customise your plot by providing various optional arguments to the plot function along with your data. If you don’t want a solid line for your graph, you can use the argument linestyle to get a dashed or dotted plot. You can also set the line width and color. The arguments related to marker face allow you to change the style of the markers that would be visible on the plot. You can have have dots, diamonds,or a simple cross as your marker. You can see the matplotlib documentation page for all available options. You might want to convey the viewer what data is on the horizontal and vertical axis. For this you can use xlabel and ylabel. You can use the legend function to put a legend on your figure. This is very helpful if you are plotting more than one graph in the same figure. From legend one can easily know which plotting style is used for which graph.