Bar Plotting

Bar Plotting

In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
ages = np.array([5, 25, 45, 20, 15])
names = np.array(['John', 'Maria', 'Priyanka', 'Martha', 'Matts'])
In [3]:
plt.bar(names, ages)
plt.show()
In [4]:
plt.bar(names, ages, color = "red")
plt.show()
In [5]:
color = np.array(["red", "purple", "green", "yellow", "blue"])
In [6]:
plt.bar(names, ages, color = color)
plt.show()

Bar graph title and x & y label

In [7]:
plt.bar(names, ages, color = color)

plt.title("Name and Age Bar Graph")
plt.xlabel("Names")
plt.ylabel("Ages")

plt.show()
In [8]:
plt.bar(names, ages, color = color)

plt.title("Name and Age Bar Graph", fontsize = 20)
plt.xlabel("Names", fontsize = 15)
plt.ylabel("Ages", fontsize = 15)

plt.show()

Plot ages and names change the x and y value

In [9]:
plt.bar(ages, names)
plt.show()

Horizental Bar

In [10]:
ages = np.array([5, 25, 45, 20, 15])
names = np.array(['John', 'Maria', 'Priyanka', 'Martha', 'Matts'])

plt.barh(names, ages)
plt.show()

Generate x any y with numpy arange()

In [11]:
x = np.arange(4)
y = np.random.rand(4)
print(x)
print(y)
[0 1 2 3]
[0.74977035 0.46075216 0.20877558 0.83256212]
In [12]:
plt.bar(x, y)
plt.show()

Combined bar graph

In [13]:
x = np.arange(5)
y = np.random.rand(4, 5)
In [14]:
print(x)
print(y)
[0 1 2 3 4]
[[0.7045655  0.71276931 0.42875423 0.32457147 0.10620684]
 [0.39211275 0.97822557 0.19020476 0.45456743 0.87602839]
 [0.6056702  0.92448795 0.97788145 0.93067779 0.44006445]
 [0.15374324 0.74571794 0.478721   0.63886297 0.36412861]]
In [15]:
plt.bar(x + 0.00, y[0], color = 'b', width = 0.25)
plt.bar(x + 0.25, y[1], color = 'g', width = 0.25)
plt.bar(x + 0.50, y[2], color = 'r', width = 0.25)
plt.show()

Plot grade and score

In [16]:
grade = ["III", "IV", "V", "VI", "VII"] 
score = [60, 40, 50, 78, 90]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score)

plt.show()
In [17]:
grade = ["III", "IV", "V", "VI", "VII"] 
score = [60, 40, 50, 78, 90]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score)

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize=20)
plt.xlabel('Grade', fontsize=15)
plt.ylabel('Score in %', fontsize=15)

plt.show()
In [18]:
grade = ["III", "IV", "V", "VI", "VII"] 
score = [60, 40, 50, 78, 90]
color = ["red", "purple", "green", "yellow", "blue"]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score, color = color, edgecolor = 'red')

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize = 20)
plt.xlabel('Grade', fontsize = 15)
plt.ylabel('Score in %', fontsize = 15)

plt.show() 

Linestyle, linewidth and alpha

linestyle description

'-' or 'solid' solid line '--' or 'dashed' dashed line '-.' or 'dashdot' dash-dotted line ':' or 'dotted' dotted line

In [19]:
grade = ["III", "IV", "V", "VI", "VII"] 
score = [60, 40, 50, 78, 90]
color = ["red", "purple", "green", "yellow", "blue"]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score, color = color, edgecolor = 'red', linestyle = '--')

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize = 20)
plt.xlabel('Grade', fontsize = 15)
plt.ylabel('Score in %', fontsize = 15)

plt.show() 
In [20]:
grade = ["III", "IV", "V", "VI", "VII"] 
score = [60, 40, 50, 78, 90]
color = ["red", "purple", "green", "yellow", "blue"]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score, color = color, edgecolor = 'red', linestyle = '--', alpha = 0.4, linewidth = 2)

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize = 20)
plt.xlabel('Grade', fontsize = 15)
plt.ylabel('Score in %', fontsize = 15)

plt.show() 

Stacked bar chart

In [21]:
grade = ["III", "IV", "V", "VI", "VII"] 
#score = [60, 40, 50, 78, 90]

score_male = [40, 60, 30, 65, 85]
score_female = [50, 70, 80, 55, 75]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score_male, color = "#00bfff", edgecolor = 'red')
plt.bar(position_of_grade, score_female, color = "#bf00ff", edgecolor = 'red', bottom = score_male)

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize = 20)
plt.xlabel('Grade', fontsize = 15)
plt.ylabel('Score in %', fontsize = 15)

plt.show() 

Legend

Location String Location Code

'best' 0 'upper right' 1 'upper left' 2 'lower left' 3 'lower right' 4 'right' 5 'center left' 6 'center right' 7 'lower center' 8 'upper center' 9 'center' 10

In [22]:
grade = ["III", "IV", "V", "VI", "VII"] 
#score = [60, 40, 50, 78, 90]

Gender=['Male','Female']

score_male = [40, 60, 30, 65, 85]
score_female = [50, 70, 80, 55, 75]

position_of_grade = np.arange(len(grade))
plt.bar(position_of_grade, score_male, color = "#00bfff", edgecolor = 'red')
plt.bar(position_of_grade, score_female, color = "#bf00ff", edgecolor = 'red', bottom = score_male)

plt.xticks(position_of_grade, grade)
plt.title('Barchart - Score by Grade',fontsize = 20)
plt.xlabel('Grade', fontsize = 15)
plt.ylabel('Score in %', fontsize = 15)

plt.legend(Gender,loc=2)

plt.show() 

Questions

A survey of 100 people asked them "Which is the nicest fruit?":

Fruit:      Apple   Orange  Banana  Mango   Watermelon  Grapes
People:     30      10      10      20      5           25
In [23]:
fruits = ["Apple", "Orange", "Banana", "Mango", "Watermelon", "Grapes"]
fruits
Out[23]:
['Apple', 'Orange', 'Banana', 'Mango', 'Watermelon', 'Grapes']
In [24]:
no_of_people = [30, 10, 10, 20, 5, 25]
no_of_people
Out[24]:
[30, 10, 10, 20, 5, 25]
In [25]:
color = ["#33f6ff", "#a5ff33", "#ee42d4", "#e7f121", "#f83a10", "#3f41f1"]
In [26]:
plt.bar(fruits, no_of_people, color = color)

plt.xticks(fruits)
plt.title('Barchart - Which is the nicest fruit?',fontsize=20)
plt.xlabel('Fruits', fontsize=15)
plt.ylabel('Number of People', fontsize=15)
plt.show()

Plot Literacy Rate in India

Top Performing States in 2018 No State Literacy Rate in Percentage 1 Kerala 96.2% 2 Delhi 88.7% 3 Uttarakhand 87.6% 4 Himachal Pradesh 86.6% 5 Assam 85.9% 6 Maharashtra 84.8%

In [27]:
states = ["Kerala", "Delhi", "Uttarakhand", "Himachal Pradesh", "Assam", "Maharashtra"]
states
Out[27]:
['Kerala', 'Delhi', 'Uttarakhand', 'Himachal Pradesh', 'Assam', 'Maharashtra']
In [28]:
literacy_rate = [96.2, 88.7, 87.6, 86.6, 85.9, 84.8]
literacy_rate
Out[28]:
[96.2, 88.7, 87.6, 86.6, 85.9, 84.8]
In [29]:
color = ["#33f6ff", "#a5ff33", "#ee42d4", "#e7f121", "#f83a10", "#3f41f1"]
In [30]:
plt.figure(figsize=(10, 6))
plt.bar(states, literacy_rate, color = color)

plt.xticks(states)
plt.title('Top Performing States in 2018',fontsize=20)
plt.xlabel('State', fontsize=15)
plt.ylabel('Literacy Rate in Percentage', fontsize=15)
plt.show()

Plot comparative literacy statistics on country

UNESCO list of countries by literacy rate (2015)

The adult and youth literacy rates for India and some neighboring countries in 2015. Adult literacy rate is based on the 15+ years age group, while the youth literacy rate is for the 15–24 years age group.

UNESCO list of countries by literacy rate (2015) Country Adult literacy rate Youth literacy rate ages 15–24 China 96.4% 99.7% Sri Lanka 92.6% 98.8% Myanmar 93.7% 96.3% World average 86.3% 91.2% India 81% 91.66% Nepal 64.7% 86.9% Bangladesh 61.5% 83.2% Pakistan 58% 80.3%

This data is taken from wikipedia https://en.wikipedia.org/wiki/Literacy_in_India.

In [31]:
countries = ["China", "Sri Lanka", "Myanmar", "World average", "India", "Nepal", "Bangladesh", "Pakistan"]
In [32]:
adult_literacy_rate = [96.4, 92.6, 93.7, 86.3, 81, 64.7, 61.5, 58]
In [33]:
color = ["#33f6ff", "#a5ff33", "#ee42d4", "#e7f121", "#f83a10", "#3f41f1", "#ef24d4", "#e3f124"]
In [34]:
plt.figure(figsize=(10, 6))
plt.bar(countries, adult_literacy_rate, color = color)

plt.xticks(countries)
plt.title('UNESCO list of countries by literacy rate (2015) ',fontsize=20)
plt.xlabel('Countries', fontsize=15)
plt.ylabel('Adult Literacy Rate in Percentage', fontsize=15)
plt.show()

Plot countries and adult youth literacy rate

In [35]:
adult_literacy_rate_ages_15_to_24 = [99.7, 98.8, 96.3, 91.2, 91.66, 86.9, 83.2, 80.3]
In [36]:
import seaborn as sns

colors= sns.color_palette("husl", 8)
colors
Out[36]:
In [37]:
plt.figure(figsize=(10, 6))
plt.bar(countries, adult_literacy_rate_ages_15_to_24, color = colors)

plt.xticks(countries)
plt.title('UNESCO List of Countries by Literacy Rate Ages 15–24 (2015) ',fontsize=18)
plt.xlabel('Countries', fontsize=12)
plt.ylabel('Youth Literacy Rate Ages 15–24 in Percentage', fontsize=12)
plt.show()

Subplots

In [38]:
fig, (ax1, ax2) = plt.subplots(1, 2)
fig.set_figwidth(20)

ax1.bar(countries, adult_literacy_rate, color = color)
ax1.set_title("Adult Literacy Rate")

ax2.bar(countries, adult_literacy_rate_ages_15_to_24, color = color)
ax2.set_title("Youth Literacy Rate")

fig.suptitle('UNESCO List of Countries by Literacy Rate Ages 15+ and 15–24 (2015)', fontsize = 20)

plt.show()

Machine Learning

  1. Deal Banking Marketing Campaign Dataset With Machine Learning

TensorFlow

  1. Difference Between Scalar, Vector, Matrix and Tensor
  2. TensorFlow Deep Learning Model With IRIS Dataset
  3. Sequence to Sequence Learning With Neural Networks To Perform Number Addition
  4. Image Classification Model MobileNet V2 from TensorFlow Hub
  5. Step by Step Intent Recognition With BERT
  6. Sentiment Analysis for Hotel Reviews With NLTK and Keras
  7. Simple Sequence Prediction With LSTM
  8. Image Classification With ResNet50 Model
  9. Predict Amazon Inc Stock Price with Machine Learning
  10. Predict Diabetes With Machine Learning Algorithms
  11. TensorFlow Build Custom Convolutional Neural Network With MNIST Dataset
  12. Deal Banking Marketing Campaign Dataset With Machine Learning

PySpark

  1. How to Parallelize and Distribute Collection in PySpark
  2. Role of StringIndexer and Pipelines in PySpark ML Feature - Part 1
  3. Role of OneHotEncoder and Pipelines in PySpark ML Feature - Part 2
  4. Feature Transformer VectorAssembler in PySpark ML Feature - Part 3
  5. Logistic Regression in PySpark (ML Feature) with Breast Cancer Data Set

PyTorch

  1. Build the Neural Network with PyTorch
  2. Image Classification with PyTorch
  3. Twitter Sentiment Classification In PyTorch
  4. Training an Image Classifier in Pytorch

Natural Language Processing

  1. Spelling Correction Of The Text Data In Natural Language Processing
  2. Handling Text For Machine Learning
  3. Extracting Text From PDF File in Python Using PyPDF2
  4. How to Collect Data Using Twitter API V2 For Natural Language Processing
  5. Converting Text to Features in Natural Language Processing
  6. Extract A Noun Phrase For A Sentence In Natural Language Processing