Showing posts with label matplotlib. Show all posts
Showing posts with label matplotlib. Show all posts

Matplotlib - plotting a dictionary using matplot lib



 # Dictionary - Histograms and matplotlib


# Create a histogram based on the frequency of characters in given string


# Histogram - String - Word Frequency


import matplotlib.pyplot as plt


import operator


string1 = "An algorithm is a step-by-step procedure to solve a given problem. It is a well-defined computational procedure that takes some values as input, manipulates them using a set of instructions and produces some values as output and terminates in a finite amount of time. An algorithm, when formally written in a programming language is called a program, or code. Derivation of an algorithm that solves the problem and conversion of the algorithm into code, together,  is known as Algorithmic Problem Solving."



string1 = string1.lower()


ch = set(string1)


D = {c:string1.count(c) for c in ch if c.isalpha()}  


print("Histogram")

print("Length of dictionary = ", len(D))

print(D)


plt.bar(range(len(D)), list(D.values()), align='center')

plt.xticks(range(len(D)), list(D.keys()))


plt.xlabel('Character')

plt.ylabel('Frequency')

plt.title('Character Frequency in given string.')


plt.show()


"""

Output

>python matplot.py

Histogram

Length of dictionary =  22

{'k': 2, 'e': 40, 'n': 30, 'g': 14, 's': 26, 'u': 14, 'o': 38, 'i': 32, 't': 37, 'r': 25, 'l': 22, 'b': 4, 'w': 4, 'f': 7, 'p': 14, 'v': 8, 'a': 39, 'y': 2, 'm': 20, 'd': 12, 'c': 10, 'h': 12}


"""

matplotlib bar chart

 # Creating a simple barchart using matplotlib


# importing the required module 

import matplotlib.pyplot as plt 


# plotting the x and y axis values for two sets of data


plt.bar([1,2,3,4,5,6,7],[7.1,9.4,10.6,12.5,11.5,10.9,9.8], label="Chennai",width=.25)


plt.bar([1.25,2.25,3.25,4.25,5.25,6.25,7.25],[8.1,10.4,9.6,8.5,10.5,8.9,7.8], label="Kancheepuram",width=.25) 


# Naming the axes and setting the legends

plt.legend()

plt.xlabel('Days')

plt.ylabel('Rainfall (mm)')


# Graph title

plt.title('Rainfall')


# Display the graph

plt.show()

"""

Output



"""


Matplotlib line graph

# Creating a line graph using matplotlib


 """

Before executing the program install matplotlib


open command prompt and type


    pip install matplotlib

   

this will install all required packages


"""


# importing the required module 

import matplotlib.pyplot as plt 

    

# x axis values 

x = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'] 


# corresponding y axis values 

y1 = [24.3, 25.3, 27.4, 29.6, 30.8, 30.3, 30, 29.2, 28.7, 27.3, 25.7, 24.6] 

y2 = [23.9, 25.8, 28.1, 28.9, 27.5, 25.2, 24.5, 24.5, 25, 24.8, 23.9, 23.2]  


# Setting the width and height of graph

f = plt.figure()

f.set_figwidth(12)

f.set_figheight(4)


# plotting the points  

plt.plot(x, y1, 'g', label='Chennai', linewidth=2.5) 

plt.plot(x, y2, 'r', label='Coimbatore', linewidth=2.5)

    

# naming the x axis 

plt.xlabel('Month') 


# naming the y axis 

plt.ylabel('Temperature') 


# display legend

plt.legend()

    

# giving a title to my graph 

plt.title('Monthly Avarage Temperature') 

    

# function to show the plot 

plt.show() 


"""

Output



"""





Matplotlib example - ploting a triangle

 # Creating a simple triangle using matplotlib


"""

Before executing the program install matplotlib

open command prompt and type


    pip install matplotlib

   

this will install all required packages

"""


# importing the required module 

import matplotlib.pyplot as plt 

    

# x axis values 

x = [1,2,3,1] 


# corresponding y axis values 

y = [2,4,1,2] 

    

# plotting the points  

plt.plot(x, y) 

    

# naming the x axis 

plt.xlabel('x - axis') 


# naming the y axis 

plt.ylabel('y - axis') 

    

# giving a title to my graph 

plt.title('My first Triangle!') 

    

# function to show the plot 

plt.show() 


Output: