# Python Chart Matplotlib

Python is the most popular use in data analysis. where data scientists looking for a way to visualize their data which show the graphic result of there code output get a better visualize of the data or display the data to convey their graphics results to someone. With the help of the Python Matplotlib module, we can plot the graph to visualize the data more effective way. Plotting the graph is quite easy by using Matplotlib. Matplotlib is a useful 2D graph library for the Python language. Humans are very visual creatures: we understand things better when we see things visualized. Graphs make it easier to see the relation between a data variable with other. It provides an interface that is easy to get started for a beginner, and it also allows to customize almost every part of a plot. In 2012 John D. Hunter has written the Matplotlib module and Matplotlib ver. 1.5.1 is a most stable version available nowadays. To install the Matplotlib python module, first, have a look of Matplotlib.org and download the version that matches with your installed Python version and runs below command to install matplotlib module.
`Pip install matplotlib`

Matplotlib is capable of creating all manner of charts, graphs, plots, histograms, and much more.

In this tutorial will cover some of the known charts types like the Scatter Chart, line chart, Parabola chart, Bar chart, Pie chart, Treemap, and others. ## Matplotlib Examples

### Scatter Chart :

Scatter plots are good for defining the relationship among two variables, To make a scatter chart using matplotlib, we will use the scatter() function. It requires two values, which represent the X and Y axis.

` ` ` from matplotlib import pyplot as plt # Define X and Y values X = [299,347,720,233,314,350,560,330,170,620,770,250] Y = [20,26,29,52,51,32,64,35,48,67,78,58] #Define Plot type plt.scatter(X,Y) #Show Chart plt.show() ` ` ` ` `

Original plot might not look good by using some some properties for plt.scatter we can change the colour, sipe and size of points and can plot more grasefull graph.

plt.scatter() paremeters :-

s: size of point, default = 20

marker: point symbol, default = ‘o’

c:  sequence of color, default = ‘b’

`plt.scatter(X, Y, s=60, c='red', marker='^')`
` ` ` from matplotlib import pyplot as plt X = [299,347,720,233,314,350,560,330,170,620,770,250] Y = [20,26,29,52,51,32,64,35,48,67,78,58] #scatter plot plt.scatter(X, Y, s=90, c='blue', marker='^') #change axes plt.xlim(0,1000) plt.ylim(0,100) #add title plt.title('Temperature and Juice Sales Relationship') #Define x and y labels plt.xlabel('No of Juice Sold') plt.ylabel('Temperature') #show plot plt.show() ` ` ` ` `

### Linear Chart :

We know that any linear equation with two variables can be written in the form Y = Mx + C
and that its graph is a line, Linear chart are most widelly used in everyday life. These chart show the relation of Y axis value with respect to X axis values, when you connect all the point it bacome a straight line.

` ` ` # Import the necessary packages and modules from matplotlib import pyplot as plt #Plotting to our canvas plt.plot([1,2,3],[2,4,6]) # Show the plot plt.show() ` ` ` ` `

Original graph looks very basic but if want to add some style

once download complete extract the folder into c:/python34/matplotlib, check your python version first under c: drive, or if you are not using windows system make sure style folder extract under root folder.

Plot two line with a given dataset. In below code, we have used style ggplot with style module. plt.plot(x,y,linewidth=5) use to give linewidth and to give title of chart use plt.title(‘Linear Chart’) to give the X and Y axis label plt.ylabel(‘Y axis’) plt.xlabel(‘X axis’)

` ` ` # Import the necessary packages and modules from matplotlib import pyplot as plt from matplotlib import style #Style chart style.use('ggplot') x = [1,4,8] y = [2,14,28] x2 = [0,9,12] y2 = [9,18,21] plt.plot(x,y,'g',label='line one', linewidth=5) plt.plot(x2,y2,'c',label='line two',linewidth=6) # Title name and Label plt.title('Linear Chart') plt.ylabel('Y axis') plt.xlabel('X axis') plt.legend() plt.grid(True,color='darkgreen') plt.show() ` ` ` ` `

### Bar Chart :

Bar charts with matplotlib are same as scatter plots. The only major change is bar charts is to center them, A bar chart is a plot/graph with rectangular bars. The graph typically compares different groups. While the graphs can be plotted vertically (bars chart standing up) or horizontally (bars chart laying flat from left to right), the most typical type of bar graph is vertical.

` ` ` from matplotlib import pyplot as plt from matplotlib import style style.use('ggplot') x = [6,9,11] y = [13,17,4] x2 = [5,8,10] y2 = [5,12,7] plt.bar(x, y, align='center') plt.bar(x2, y2, color='b', align='center') plt.title('Bar Chart') plt.ylabel('Y axis') plt.xlabel('X axis') plt.show() ` ` ` ` `
` ` ` import numpy as np import matplotlib.pyplot as plt # Make a dataset height = [3, 12, 5, 18, 45] bars = ('A', 'B', 'C', 'D', 'E') y_pos1 = np.arange(len(bars)) plt.bar(y_pos1, height, color=(0.2, 0.4, 0.6, 0.6)) plt.xticks(y_pos1, bars) plt.show() ` ` ` ` `

### Parabola Chart :

Parabola Chart are not use line equation Y = Mx + C it use some complex equation to plot the graph

` ` ` import matplotlib.pyplot as plt x = range(-10,10) y = [x*x for x in x] plt.scatter(x, y) plt.plot(x,y,color='y') plt.show() ` ` ` ` `

### Pie Chart :

Pie Chart divided circle into proportional segments and show proportions and percentages between categories. The full circle show the total sum of all the data, equal to 100%.

via GIPHY

` ` ` import matplotlib.pyplot as plt # Data to plot labels = 'Python', 'C++', 'Ruby', 'Java' sizes = [315, 130, 145, 220] colors = ['yellowgreen' ,'gold', 'lightcoral', 'lightskyblue'] explode = (0.1, 0, 0, 0) # blow up 1st slice # Plot plt.pie(sizes, explode=explode, labels=labels, colors=colors, autopct='%1.1f%%', shadow=True, startangle=140) plt.axis('equal') plt.show() ` ` ` ` `

### HeatMap Chart :

A heat map chart is a specialized chart which uses colors to represent data values in a table. It most useful when you need to plot large and complex data.

` ` ` import seaborn as sns import pandas as pd import numpy as np Create a dataset (fake) df = pd.DataFrame(np.random.random((4,4)), columns=["a","b","c","d"]) Default heatmap: just a visualization of this square matrix p1 = sns.heatmap(df) ` ` ` ` `

### Tree Chart :

Tree chart is a type of graphic organizer that shows how items are related to one another. Three chart havin branches, leaf node, sub branches and this graph will give good reasult in labled data.

` ` ` import matplotlib.pyplot as plt import squarify # (algorithm for treemap) # Change color squarify.plot(sizes=[12,23,36,6], label=["group A", "group B", "group C", "group D"], color=["red","green","blue", "grey"], alpha=.4 ) plt.axis('off') plt.show() ` ` ` ` `

So these are the basic example of the different graphs in python with the help of Matplotlib module.