To plot stacked bar plot with logarithmic scale in Pandas we can use parameters:
stacked=True
log=True
Steps to plot bar chart - stacked and log
- Import matplotlib library
- Create DataFrame with correlated data
- Create the bar plot
- add parameters:
stacked=True, log=True
More information can be found: DataFrame.plot
Data
amount | price | |
---|---|---|
A | 4 | 400 |
B | 8 | 500 |
C | 15 | 1000 |
D | 7 | 600 |
E | 3 | 120 |
Example
import matplotlib.pyplot as plt
import pandas as pd
data = {"amount":{"A":4,"B":8,"C":15,"D":7,"E":3},
"price":{"A":400,"B":500,"C":1000,"D":600,"E":120}}
df = pd.DataFrame(data)
df.plot.bar(secondary_y= 'amount', stacked=True, log=True)
ax1, ax2 = plt.gcf().get_axes()
ax1.set_ylabel('price')
ax2.set_ylabel('amount')
Output
Example 2 - non logarithmic
Create a figure and two axes:
import matplotlib.pyplot as plt
import pandas as pd
data = {"amount":{"A":4,"B":8,"C":15,"D":7,"E":3},
"price":{"A":400,"B":500,"C":1000,"D":600,"E":120}}
df = pd.DataFrame(data)
_ = df.plot( kind= 'bar' , secondary_y= 'amount' , rot= 0 , stacked=True, log=False)
plt.show()