Prev: Modifying Rows in DataFrame | Next: The power of 'GroupBy'
s.plot() or df.plot()
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
s = pd.Series([np.random.randint(1,100) for i in range(1,100)])
p = plt.plot(s.index, s.values)
s = pd.Series(np.sin(np.linspace(0, 2*np.pi, 100)), index=np.linspace(0, 2*np.pi, 100))
plt.xticks(np.linspace(0, 2*np.pi, 5),('0', 'Ï€/2', 'Ï€', '3Ï€/2', '2Ï€'))
p = plt.plot(s.index, s.values)
s = pd.Series([np.random.randint(1,100) for i in range(1,100)])
n, bins, patches = plt.hist(s.values, color='cyan', alpha=0.5)
s = pd.Series([np.random.randint(1,100) for i in range(1,100)])
p = s.plot(kind='hist', color='r', alpha=0.5)
First make sure that you run the code below so that df is properly constructed to further run the examples
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
df = pd.read_csv("../../data/sampledata.csv",sep=',',skiprows=3, header=0, index_col=0)
#delete column 'Country' to only have columns with float data
del df['Country']
df = df/1e6
df
s = df.xs('2015', axis=1)
s
# plotting a barchart
p1 = s.plot(kind='bar', title='Countries population in millions in 2005', yticks=[10, 40, 80])
s = df.xs('ESP')
p = s.plot()
p1 = s.plot(kind='bar', ylim=(44,47))
p2 = s.plot(kind='line', colormap='Reds_r', title='Population of Spain')
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
data = {'country': ['Italy','Spain','Greece','France','Portugal'],
'popu': [61, 46, 11, 65, 10],
'percent': [0.83,0.63,0.15,0.88,0.14],
'area': [301,506,132,641,92]}
df = pd.DataFrame(data, index=['ITA', 'ESP', 'GRC', 'FRA', 'PRT'])
df.sort_values('country', axis=0, ascending=True, inplace=True)
df
df.plot('area', 'popu', kind='scatter')
df.plot(df.index, 'area', kind='bar')
# multiple plots
fig, axes = plt.subplots(nrows=2, ncols=1)
p1 = df['popu'].plot(kind='bar', ax=axes[0], figsize=(10, 10), title='Population', alpha=0.5)
p2 = df['area'].plot(kind='bar', ax=axes[1], figsize=(10, 10), title='Area', color='r', alpha=0.5)
. Free learning material
. See full copyright and disclaimer notice