Q. Given a data frame df1 as shown below:
1990 2000 2010
a
52 340 890
b
64 480 560
c
78 688 1102
d
94 766 889
Write code to create:
(a) A scatter chart from the 1990 and
2010 columns of dataframe df1
(b) A line chart from the 1990 and 2000
columns of dataframe df1
(c) Create a bar chart plotting the three
columns of dataframe dfl
Answer :-
(a)
import pandas as pd
import matplotlib.pyplot as plt
d={1990:[52,64,78,94],2000:[340,480,688,766],2010:[890,560,1102,889]}
df1=pd.DataFrame(d,index=list("abcd"))
df1.plot.scatter(x = 1990, y = 2010);
plt. show()
Output :-
(b)
import pandas as pd
import matplotlib.pyplot as plt
d={1990:[52,64,78,94],2000:[340,480,688,766],2010:[890,560,1102,889]}
df1=pd.DataFrame(d,index=list("abcd"))
df1.plot(x=1990, y=2000);
plt. show()
Output :-
(c)
import pandas as pd
import matplotlib.pyplot as plt
d={1990:[52,64,78,94],2000:[340,480,688,766],2010:[890,560,1102,889]}
df1=pd.DataFrame(d,index=list("abcd"))
df1.plot.bar()
plt.show()
Output :-
For making graph only without using Datafamr :-
Note :- Below scripts are not answers of the above question these are
additional information.
(a)
import matplotlib.pyplot as plt
import numpy as np
X = [1990, 2000, 2010]
a = [52, 340, 890]
b = [ 64, 480, 560 ]
c = [ 78, 688, 1102 ]
d = [ 94 , 766, 889 ]
plt.scatter(X, a)
plt.scatter(X, b)
plt.scatter(X, c)
plt.scatter(X, d)
plt.xlabel("Year")
plt. show()
Output :-
(b)
import matplotlib.pyplot as plt
import numpy as np
X = [1990, 2000, 2010]
a = [52, 340, 890]
b = [ 64, 480, 560 ]
c = [ 78, 688, 1102 ]
d = [ 94 , 766, 889 ]
plt.plot(X, a)
plt.plot(X, b)
plt.plot(X, c)
plt.plot(X, d)
plt.xlabel("Year")
plt. show()
Output :-
import matplotlib.pyplot as plt
import numpy as np
X = np. arange( 1990 , 2011 ,10)
a = [52, 340, 890]
b = [ 64, 480, 560 ]
c = [ 78, 688, 1102 ]
d = [ 94 , 766, 889 ]
plt.bar(X , a)
plt.bar(X + 1 , b)
plt.bar(X + 2, c)
plt.bar(X + 3, d)
plt.xlabel("Year")
plt. show()
Output :-






Extremely helpful! 😃
ReplyDeletePost a Comment
You can help us by Clicking on ads. ^_^
Please do not send spam comment : )