In this post, I will teach you how to plot a dataframe you created in Pandas.
This is my sample dataframe whose graph I would like to draw.
import pandas as pd my_data = {'Tax_Rate': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], 'Taxes_Collected': [100000000, 120000000, 170000000, 260000000, 210000000, 80000000, 40000000]} my_df = pd.DataFrame(my_data)
I wanted to represent the Laffer curve, so I chose lines and bars.
To plot the chart I will use the matplotlib module which I need to import.
import pandas as pd import matplotlib.pyplot as plt my_data = {'Tax_Rate': [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7], 'Taxes_Collected': [100000000, 120000000, 170000000, 260000000, 210000000, 80000000, 40000000]} my_df = pd.DataFrame(my_data) my_df.plot.bar(x='Tax_Rate', y='Taxes_Collected') plt.plot(my_df) plt.show()
This is the simplest plot you can create from a dataframe using matplotlib. The matplotlib module also allows you to expand the plot further to suit your needs.
See also:
Matplotlib plot documentation
How to check if Pandas dataframe is empty
How to insert bar chart in Excel
1 thought on “How to plot a dataframe”