. Plotting In Python

[5]:
123
import matplotlib.pyplot as plt
import pandas as pd
[2]:
12345678
# plotting simple line chart
import numpy as np

xpoints = np.array([0, 6])
ypoints = np.array([0, 250])

plt.plot(xpoints, ypoints)
plt.show()
output
[7]:
12
df=pd.read_csv('./data/fifa_eda.csv') # reading fifa data this file is saved in my current directory/data folder
df.head(1)
ID Name Age Nationality Overall Potential Club Value Wage Preferred Foot International Reputation Skill Moves Position Joined Contract Valid Until Height Weight Release Clause
0 158023 L. Messi 31 Argentina 94 94 FC Barcelona 110500.0 565.0 Left 5.0 4.0 RF 2004 2021-01-01 5.583333 159.0 226500.0
[9]:
123
x=df['Height']
y=df['Weight']
plt.scatter(x,y)
Out[9]:
<matplotlib.collections.PathCollection at 0x7fd54a16ac10>
output
[21]:
1234
# Plotting age wise average wage in line chart 
tmp_df=df.groupby(['Age'])['Wage'].mean().reset_index()
plt.plot(tmp_df['Age'],tmp_df['Wage'],marker='o',color='b')
Out[21]:
[<matplotlib.lines.Line2D at 0x7fd54c78d400>]
output
[22]:
12345678
# drawing multiple line charts
y1 = np.array([3, 8, 1, 10])
y2 = np.array([6, 2, 7, 11])

plt.plot(y1)
plt.plot(y2)

plt.show()
output
[25]:
1234567
# Plotting age wise average wage in line chart 
tmp_df=df.groupby(['Age'])['Wage'].mean().reset_index()
plt.plot(tmp_df['Age'],tmp_df['Wage'],marker='o',color='b')
plt.title("Age wise average wage")
plt.grid()
plt.xlabel('Age')
plt.ylabel('Avg Wage')
Out[25]:
Text(0, 0.5, 'Avg Wage')
output
[31]:
123456789
# Plotting age wise average wage in line chart 
tmp_df=df.groupby(['Age'])['Wage'].mean().reset_index()
plt.figure(figsize=(20,8))
plt.plot(tmp_df['Age'],tmp_df['Wage'],marker='o',color='b')

plt.title("Age wise average wage")
plt.grid()
plt.xlabel('Age')
plt.ylabel('Avg Wage')
Out[31]:
Text(0, 0.5, 'Avg Wage')
output
[36]:
12345678
plt.subplot(1,2,1)
tmp_df1=df.groupby(['Age'])['Wage'].mean().reset_index()
tmp_df2=df.groupby(['Age'])['Weight'].mean().reset_index()
plt.plot(tmp_df1['Age'],tmp_df1['Wage'])
plt.title("title1")
plt.subplot(1,2,2)
plt.plot(tmp_df2['Age'],tmp_df2['Weight'])
plt.title("title2")
Out[36]:
Text(0.5, 1.0, 'title2')
output
[37]:
123456789
# subplot with scatter
plt.subplot(1,2,1)
tmp_df1=df.groupby(['Age'])['Wage'].mean().reset_index()
tmp_df2=df.groupby(['Age'])['Weight'].mean().reset_index()
plt.scatter(tmp_df1['Age'],tmp_df1['Wage'])
plt.title("title1")
plt.subplot(1,2,2)
plt.scatter(tmp_df2['Age'],tmp_df2['Weight'])
plt.title("title2")
Out[37]:
Text(0.5, 1.0, 'title2')
output
[38]:
1234567891011
#day one, the age and speed of 13 cars:
x = np.array([5,7,8,7,2,17,2,9,4,11,12,9,6])
y = np.array([99,86,87,88,111,86,103,87,94,78,77,85,86])
plt.scatter(x, y)

#day two, the age and speed of 15 cars:
x = np.array([2,2,8,1,15,8,12,9,7,3,11,4,7,14,12])
y = np.array([100,105,84,105,90,99,90,95,94,100,79,112,91,80,85])
plt.scatter(x, y)

plt.show()
output
[43]:
123
# player position wise average wage
tmp_df=df.groupby(['Position'])['Wage'].mean().reset_index()
tmp_df.head()
Position Wage
0 CAM 10.218978
1 CB 7.700393
2 CDM 9.315401
3 CF 10.216216
4 CM 8.334767
[50]:
123
plt.figure(figsize=(20,8))
plt.bar(x=tmp_df['Position'],height=tmp_df['Wage'],color='b')
Out[50]:
<BarContainer object of 27 artists>
output
[54]:
123
# histograme of age
plt.figure(figsize=(20,8))
plt.hist(df['Age'],bins=len(df['Age'].unique()))
Out[54]:
(array([4.200e+01, 2.890e+02, 7.320e+02, 1.024e+03, 1.240e+03, 1.423e+03, 1.340e+03, 1.332e+03, 1.358e+03, 1.319e+03, 1.387e+03, 1.162e+03, 1.101e+03, 9.590e+02, 9.170e+02, 7.070e+02, 5.740e+02, 4.080e+02, 4.040e+02, 1.960e+02, 1.270e+02, 8.200e+01, 3.700e+01, 2.500e+01, 1.300e+01, 5.000e+00, 1.000e+00, 0.000e+00, 3.000e+00]), array([16., 17., 18., 19., 20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32., 33., 34., 35., 36., 37., 38., 39., 40., 41., 42., 43., 44., 45.]), <BarContainer object of 29 artists>)
output
[55]:
1
import seaborn as sns 
[58]:
12
plt.figure(figsize=(20,8))
sns.histplot(df['Age'],bins=len(df['Age'].unique()))
Out[58]:
<AxesSubplot:xlabel='Age', ylabel='Count'>
output
[ ]:
1