In this article, you will find Important Programs Plotting with Python Class 12. You can use these programs in your practical file as practical records.
Important Programs Plotting with Python Class 12
In this section of important programs plotting with python class 12, we will cover practicals to plot a single line chart.
[1] Plot following data on line chart:
Day | Monday | Tuesday | Wednesday | Thursday | Friday |
Income | 510 | 350 | 475 | 580 | 600 |
- Write a title for the chart “The Weekly Income Report”.
- Write the appropriate titles of both the axes.
- Write code to Display legends.
- Display red color for the line.
- Use the line style – dashed
- Display diamond style markers on data points
import matplotlib.pyplot as pp
day =['Monday','Tuesday','Wednesday','Thursday','Friday']
inc = [510,350,475,580,600]
pp.plot(day,inc,label='Income',color='r',linestyle='dashed',marker='D')
pp.title("The Weekly Income Report")
pp.xlabel("Days")
pp.ylabel("Income")
pp.legend()
pp.show()
Output:
[2] A Shivalik restaurant has recorded the following data into their register for their income by Drinks and Food. Plot them on the line chart.
Day | Monday | Tuesday | Wednesday | Thursday | Friday |
Drinks | 450 | 560 | 400 | 605 | 580 |
Food | 490 | 600 | 425 | 610 | 625 |
Apply following customization to the line chart.
- Write a title for the chart “The Weekly Restaurant Orders”.
- Write the appropriate titles of both the axes.
- Write code to Display legends.
- Display your choice of colors for both the lines drinks and food.
- Use the line style – dotted for drinks and dashdot for food.
- Display plus markers on drinks and x markers of food.
import matplotlib.pyplot as pp
day =['Monday','Tuesday','Wednesday','Thursday','Friday']
dr = [450,560,400,605,580]
fd = [490,600,425,610,625]
pp.plot(day,dr,label='Drinks',color='g',linestyle='dotted',marker='+')
pp.plot(day,fd,label='Food',color='m',linestyle='dashdot',marker='x')
pp.title("The Weekly Restaurant Orders")
pp.xlabel("Days")
pp.ylabel("Orders")
pp.legend()
pp.show()
Output:
[3] Observe the given data for monthly views of one of the youtube channels for 6 months. Plot them on the line chart.
Month | January | February | March | April | May | June |
Views | 2500 | 2100 | 1700 | 3500 | 3000 | 3800 |
Apply following customizations to the chart:
- Give the title for the chart – “Youtube Stats”
- Use the “Month” label for X-Axis and “Views” for Y-Axis.
- Display legends.
- Use dashed lines with the width 5 point.
- Use red color for the line.
- Use dot marker with blue edge color and black fill color.
import matplotlib.pyplot as pp
mon =['January','February','March','April','May','June']
views = [2500,2100,1700,3500,3000,3800]
pp.plot(mon,views,label='Views State',color='r',linestyle='dashed', linewidth=4,\
marker='o', markerfacecolor='k', markeredgecolor='b')
pp.title("Youtube Stats")
pp.xlabel("Months")
pp.ylabel("Views")
pp.legend()
pp.show()
Output:
[4] Consider the following data of a medical store and plot the data on the line chart:
Month | Masks | Sanitizer | Hand wash |
March | 1500 | 4400 | 6500 |
April | 3500 | 4500 | 5000 |
May | 6500 | 5500 | 5800 |
June | 6700 | 6000 | 6300 |
July | 6000 | 5600 | 6200 |
August | 6800 | 6300 | 4500 |
Customize the chart as you wish.
import matplotlib.pyplot as pp
mon =['March','April','May','June','July','August']
mask= [1500,3500,6500,6700,6000,6800]
san = [4400,4500,5500,6000,5600,6300]
hw = [6500,5000,5800,6300,6200,4500]
pp.plot(mon,mask,label='Mask',color='g',linestyle='dashed', linewidth=4,\
marker='o', markerfacecolor='k', markeredgecolor='r')
pp.plot(mon,san,label='Mask',color='b',linestyle='dashed', linewidth=4,\
marker='3', markerfacecolor='k', markeredgecolor='g')
pp.plot(mon,hw,label='Mask',color='r',linestyle='dashed', linewidth=4,\
marker='v', markerfacecolor='k', markeredgecolor='b')
pp.title("Fitwell Medical Store")
pp.xlabel("Months")
pp.ylabel("Covid Protections")
pp.legend()
pp.show()
Output:
[5] Use above data and subplot sanitizer data and handwash data.
import matplotlib.pyplot as pp
mon =['March','April','May','June','July','August']
san = [4400,4500,5500,6000,5600,6300]
hw = [6500,5000,5800,6300,6200,4500]
pp.subplot(2,1,1)
pp.plot(mon,san,label='Sanitizer',color='b',linestyle='dashed', linewidth=4,\
marker='o', markerfacecolor='k', markeredgecolor='r')
pp.title("Fitwell Medical Store")
pp.legend()
pp.subplot(2,1,2)
pp.plot(mon,hw,label='Handwash',color='r',linestyle='dashed', linewidth=4,\
marker='v', markerfacecolor='k', markeredgecolor='b')
pp.xlabel("Months")
pp.ylabel("Covid Protections")
pp.legend()
pp.show()
Output:
[6] Write a program to plot a range from 1 to 30 with step value 4. Use the following algebraic expression to show data.
y = 5*x+2
import matplotlib.pyplot as pp
import numpy as np
x = np.arange(1,30,4)
y = 5 * x + 2
pp.plot(x,y)
pp.show()
Output:
[7] Display following bowling figures through bar chart:
Overs | Runs |
1 | 6 |
2 | 18 |
3 | 10 |
4 | 5 |
import matplotlib.pyplot as pp
overs =[1,2,3,4]
runs=[6,18,10,5]
pp.bar(overs,runs,color='m')
pp.xlabel('Overs')
pp.xlabel('Runs')
pp.title('Bowling Spell Analysis')
pp.xticks([1,2,3,4])
pp.yticks([5,10,15,20])
pp.show()
Output:
[8] Observe following data and plot data according to given instructions:
Batsman | 2017 | 2018 | 2019 | 2020 |
Virat Kohli | 2501 | 1855 | 2203 | 1223 |
Steve Smith | 2340 | 2250 | 2003 | 1153 |
Babar Azam | 1750 | 2147 | 1896 | 1008 |
Rohit Sharma | 1463 | 1985 | 1854 | 1638 |
Kane Williamson | 1256 | 1785 | 1874 | 1974 |
Jos Butler | 1125 | 1853 | 1769 | 1436 |
- Create a bar chart to display data of Virat Kohli & Rohit Sharma.
- Customize the chart in this manner
- Use different widths
- Use different colors to represent different years score
- Display appropriate titles for axis and chart
- Show legends
- Customize the chart in this manner
- Create a bar chart to display data of Steve Smith, Kane Williamson & Jos Butler. Customize Chart as per your wish.
- Display data of all players for the specific year.
Follow this link for notes:
answer for 8th question?
This is given for self practice to students.
EVEN DONT KNOW THE ANSWER
Can we get the answer of the 8th one?
Did you get the answer?
Please the 8th one
Observe following data and plot data according to given instructions:
Batsman 2017 2018 2019 2020
Virat Kohli 2501 1855 2203 1223
Steve Smith 2340 2250 2003 1153
Babar Azam 1750 2147 1896 1008
Rohit Sharma 1463 1985 1854 1638
Kane Williamson 1256 1785 1874 1974
Jos Butler 1125 1853 1769 1436
1. Create a bar chart to display data of Virat Kohli & Rohit Sharma.
2. Customize the chart in this manner
2.1 . Use different widths
2.2. Use different colors to represent different years score
2.3. Display appropriate titles for axis and chart
2.4. Show legends
import numpy as np
import matplotlib.pyplot as plt
set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
set height of bar
Virat_Kohli = [2501,1855,2203,1223]
Rohit_Sharma = [1463,1985,1854,1638]
Set position of bar on X axis
br1 = np.arange(len(Virat_Kohli))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
Make the plot
plt.bar(br1, Virat_Kohli, color =’r’, width = barWidth,
edgecolor =’grey’, label =’Virat_Kohli’)
plt.bar(br2, Rohit_Sharma, color =’g’, width = barWidth,
edgecolor =’grey’, label =’Rohit_Sharma’)
Adding Xticks
plt.xlabel(‘Year’, fontweight =’bold’, fontsize = 15)
plt.ylabel(‘Runs’, fontweight =’bold’, fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Virat_Kohli))],
[ ‘2017’, ‘2018’, ‘2019’,’2020′])
plt.legend()
plt.show()
/////////////////////////////////////////////////////////////////////////////
2.5. Create a bar chart to display data of Steve Smith, Kane Williamson
& Jos Butler. Customize Chart as per your wish.
import numpy as np
import matplotlib.pyplot as plt
set width of bar
barWidth = 0.25
fig = plt.subplots(figsize =(12, 8))
set height of bar
Steve_Smith = [2340,2250,2003,1153]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]
Set position of bar on X axis
br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
Make the plot
plt.bar(br1, Steve_Smith, color =’r’, width = barWidth,
edgecolor =’grey’, label =’Steve_Smith’)
plt.bar(br2, Kane_Williamson, color =’g’, width = barWidth,
edgecolor =’grey’, label =’Kane_Williamson’)
plt.bar(br3, Jos_Butler, color =’b’, width = barWidth,
edgecolor =’grey’, label =’Jos_Butler’)
Adding Xticks
plt.xlabel(‘Years’, fontweight =’bold’, fontsize = 15)
plt.ylabel(‘Runs’, fontweight =’bold’, fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Steve_Smith))],
[‘2017’, ‘2018’, ‘2019’,’2020′])
plt.legend()
plt.show()
//////////////////////////////////////////////////////////////////////////
2.6. Display data of all players for the specific year
import numpy as np
import matplotlib.pyplot as plt
set width of bar
barWidth = 0.10
fig = plt.subplots(figsize =(12, 8))
set height of bar
Virat_Kohli = [2501,1855,2203,1223]
Steve_Smith = [2340,2250,2003,1153]
Babar_Azam = [1750,2147,1896,1008]
Rohit_Sharma = [1463,1985,1854,1638]
Kane_Williamson = [1256,1785,1874,1974 ]
Jos_Butler = [1125,1853,1769,1436]
Set position of bar on X axis
br1 = np.arange(len(Steve_Smith))
br2 = [x + barWidth for x in br1]
br3 = [x + barWidth for x in br2]
br4 = [x + barWidth for x in br3]
br5 = [x + barWidth for x in br4]
br6 = [x + barWidth for x in br5]
Make the plot
plt.bar(br1, Virat_Kohli, color =’red’, width = barWidth,
edgecolor =’grey’, label =’Virat_Kohli’)
plt.bar(br2, Steve_Smith, color =’cyan’, width = barWidth,
edgecolor =’grey’, label =’Steve_Smith’)
plt.bar(br3,Babar_Azam, color =’magenta’, width = barWidth,
edgecolor =’grey’, label =’Babar_Azam’)
plt.bar(br4, Rohit_Sharma, color =’yellow’, width = barWidth,
edgecolor =’grey’, label =’Rohit_Sharma’)
plt.bar(br5, Kane_Williamson, color =’green’, width = barWidth,
edgecolor =’grey’, label =’Kane_Williamson’)
plt.bar(br6, Jos_Butler, color =’blue’, width = barWidth,
edgecolor =’grey’, label =’Jos_Butler’)
Adding Xticks
plt.xlabel(‘Years’, fontweight =’bold’, fontsize = 15)
plt.ylabel(‘Runs’, fontweight =’bold’, fontsize = 15)
plt.xticks([r + barWidth for r in range(len(Steve_Smith))],
[‘2017’, ‘2018’, ‘2019’,’2020′])
plt.legend()
plt.show()