Wednesday, 23 March 2022

Plotting #15

MATPLOTLIB

Matplotlib is a low level graph plotting library in python that serves as a visualization utility.

Matplotlib was created by John D. Hunter.

Matplotlib is open source and we can use it freely.

Matplotlib is mostly written in python, a few segments are written in C, Objective-C and Javascript for Platform compatibility.

Matplotlib என்பது பைத்தானில் உள்ள ஒரு குறைந்த அளவிலான வரைபடத் திட்டமிடல் நூலகமாகும், இது காட்சிப்படுத்தல் பயன்பாடாக செயல்படுகிறது.

Matplotlib ஜான் டி. ஹண்டர் என்பவரால் உருவாக்கப்பட்டது.

Matplotlib திறந்த மூலமாகும், அதை நாம் சுதந்திரமாகப் பயன்படுத்தலாம்.

Matplotlib பெரும்பாலும் python இல் எழுதப்பட்டுள்ளது, ஒரு சில பிரிவுகள் C, Objective-C மற்றும் Javascript இல் பிளாட்ஃபார்ம் இணக்கத்தன்மைக்காக எழுதப்பட்டுள்ளன.


import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4, 5 ]
ls = [2, 4, 6, 8, 10]
d = [0, 1, 1, 2, 2 ]

def plot1():
plt.plot(x,ls)
plt.title('1. First Graph') ###
plt.savefig('v31.png')
plt.show()
plot1()
Simple definition for x, ls as list. Normally matplotlib is extensively used with np. 
Here we take minimum lists x, ls for plotting for easy understanding. 
Very simple import matplotlib, plot() with two declared lists, and show.
If u want you can save using savefig() method. This is written as function using def. 
You can simply call by plot1()
def plot2():
plt.plot(x,ls,'r-')
plt.title('2. Graph with Axes Labels and red')
plt.xlabel('X-Axis') ##
plt.ylabel('Y-Axis') ##
plt.savefig('v32.png')
plt.show()
plot2()
 The above plot2() will add title title and axis using plt.title() and plt.xlabel() and 
plt.ylabel() methods as shown above. see the result as below.


def plot3():
plt.plot(x,ls,'g-')
plt.title('3. Graph with Axes Labels + Grid Lines + Green')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.grid() #####
plt.savefig('v33.png')
plt.show()
plot3()
The above code uses plt.axis() method to add axis in the graph. The output will be similar to 
the one shown below:

def plot4():
fig = plt.figure(figsize=(5, 4))
plt.plot(x,ls,'r-')
plt.title('4. Check the Figure Size ')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.grid()
plt.savefig('v34.png')
plt.show()
plot4()
figure size is defined by fig = plt.figure(figsize=(5, 4)). Check he effect of this statement 
as shown below. 

def plot5():
fig = plt.figure(figsize=(5, 4))
plt.plot(x, x,'b-', x, ls,'r-', x, d, 'g-')
plt.title('5. Multiple Plots ')
plt.xlabel('X-Axis')
plt.ylabel('Y-Axis')
plt.grid()
plt.savefig('v35.png')
plt.show()
plot5()
See the multiple data can be plotted in a single graphs by the plt.plot(x, x,'b-', x, ls,'r-', x, d, 'g-')
Please notice the 'b-' to denote blue normal line, 'g-' to denote the green line. See the result:
def plot6():
# using numparray
fig = plt.figure(figsize=(5, 4))
x = np.array([1, 2, 3, 4, 5])
ls = np.array([2, 4, 6, 8, 10])
d = np.array([0, 1, 1, 2, 2])
plt.plot(x, x,'b-')
plt.plot(x, ls,'r-')
plt.plot(x, d, 'g-')
plt.title('6. Multiple Plots using np array')
plt.xlabel('Quantity')
plt.ylabel('Price')
plt.grid()
plt.savefig('v36.png')
plt.show()
plot6()
This code snippet is another way, used separate plt.plot() statements. Also data set is defined as num arrays.
See the result graph as shown below.


def plot7():
x = np.array([1, 2, 3, 4, 5])
ls = np.array([2, 4, 6, 8, 10])
d = np.array([0, 1, 1, 2, 2])

plt.title('6. MultipleSub Plots Column wise')
# plot 1:
plt.subplot(1, 2, 1)
plt.plot(x, x)
plt.title('Qty Vs Qty')

# plot 2:
plt.subplot(1, 2, 2)
plt.plot(x, ls)
plt.title('Qty Vs Price')
plt.savefig('v37.png')
plt.show()
plot7()
This plot explains the sub plots. plt.subplot(1, 2, 1) indicates the graph has 1 row, 2 columns, and this 
plot is the first plot. plt.supplot(1, 2, 2) indicates figure has 1 row, 2 columns, and this plot is the
second plot. See the graph below to depict the above code snippet.


def plot8():
x = np.array([1, 2, 3, 4, 5])
ls = np.array([2, 4, 6, 8, 10])
d = np.array([0, 1, 1, 2, 2])
plt.title('6. Sub Plots Row wise using np array')

# plot 1:
plt.subplot(2, 2, 1)
plt.plot(x, x)
plt.title('Qty Vs Qty')
    # plot 2:
plt.subplot(2, 2, 2)
plt.plot(x, ls)
plt.title('Quantity Vs Price')
plt.savefig('v38.png')
plt.show()
plot8()
The output graph is given above side by side  to compare between the snippets
plt.subplot(1, 2, 1),  plt.supplot(1, 2, 2) Vs plt.subplot(2, 2, 1) and plt.subplot(2, 2, 2)
Happy learning with AMET ODL🙋🙋🙋!!!

No comments:

Post a Comment

Making Prompts for Profile Web Site

  Prompt: Can you create prompt to craft better draft in a given topic. Response: Sure! Could you please specify the topic for which you...