Skip to content Skip to sidebar Skip to footer

Cannot Combine Bar And Line Plot Using Pandas Plot() Function

I am plotting one column of a pandas dataframe as line plot, using plot() : df.iloc[:,1].plot() and get the desired result: Now I want to plot another column of the same datafram

Solution 1:

import numpy as np
import pandas as pd
import matplotlib.pyplotas plt

some data

df = pd.DataFrame(np.random.randn(5,2))
print (df)
          0         1
0  0.008177 -0.121644
1  0.643535 -0.070786
2 -0.104024  0.872997
3 -0.033835  0.067264
4 -0.576762  0.571293

then we create an axes object (ax). Notice that we pass ax to both plots

_, ax = plt.subplots()

df[0].plot(ax=ax)
df[1].plot(kind='bar', ax=ax)

enter image description here

Post a Comment for "Cannot Combine Bar And Line Plot Using Pandas Plot() Function"