Matplotlib Line Chart
Matplotlib Line Chart
Class- XII
Chapter : 3
(Informatics practices 065)
Content
• What is Data visualization
• What is Matplotlib?
• Using PyPlot of Matplotlib Library
• Creating line chart
1. Create chart
syntax-
import matplotlib.pyplot
or Alias name
Most of the Matplotlib utilities lies under the pyplot submodule, and
are usually imported under the plt alias:
Working with PyPlot methods
The pyplot interface provides many methods for 2D
plotting of data. The matplotlib’s PyPlot interface
lets one plot data in multiple ways such as line
chart, bar chart, histogram etc.
Creating line chart
Line chart using plot() function-> A line chart or line graph is a
type of chart which displays information as a series of data points
called ‘marker, connected by straight line segments.
The PyPlot interface offers plot() function for creating a line graph.
Elements of line chart
markers
Example of line chart
import matplotlib.pyplot as plt
import numpy as np
output
xpoints = np.array([1,2,3,4])
ypoints = np.array([2,4,6,8])
plt.plot(xpoints, ypoints)
plt.show()
Show the labels of the axes
marker edgecolor
Marker type
Marker size
Line color
Line width
Line style
Changing marker type, size and color
Syntax->
Marker=<valid marker type>,markersize=<in points>,markeredgecolor=<valid color>
Example->
plt.plot(x,y, marker=‘*’, markersize=5, markeredgecolor=‘red’)
You can specify the color code next to the data being
plotted in plot() function as shown below:
Syntax->
<matplotlib.pyplot>.plot(<data1,[,data2],<color code>)
Example->
plt.plot(x,y,’r’,linewidth=4,linestyle=‘dashed’)
Or
linestyle=‘-.’ -.-.-.-.-.-.-.-.
linestyle=‘.’ …….………………..
linestyle=‘--’ - - - - - - -
linestyle=‘-’
example
import matplotlib.pyplot as plt
xpoints = [1,2,3, 4]
ypoints=[2,4,6,8]
plt.plot(xpoints, ypoints,\
marker="*",markersize=15,markeredgecolor="r",\
color='blue',linewidth=2,linestyle="dashed")
plt.xlabel("values",fontsize=15)
plt.ylabel("Doubles",fontsize=15)
plt.show()
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,'r',marker='o',markersize=10)
plt.plot(x2, y2,'c',marker='o',markersize=10)
plt.xlabel('X label')
plt.ylabel('Y label')
plt.show()
THANK
धन्यवाद! YOU!
आपका
HAVE A ददनGREAT
शुभ हो!DAY!
21