How to bar plot in Python
• 1 minPlotting is one of the fundamental tool of the modern Data Scientist.
Simple plots are made easy using the .plot() from the Pandas library
You will also need to use the matplotlib library
Installing Pandas and Matplotlib
pip install pandas matplotlib
pip3 install pandas matplotlib
Using Pandas
Pandas do have a little charting library in his toolbox. But will require matplotlib if you want to show it if you are running it from a script.
Here is the simplest example :
# We import the pandas along with the matplotlib library
import pandas as pd
import matplotlib.pyplot as plt # (Optional) if you run it in Jupyter
# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"y1" : range(0,10)})
# We plot the dataframe
df.plot(kind="bar")
# We need to show the plot.
plt.show() # (Optional) if you run it in Jupyter
You can pass quite a lot of paremeters to the .plot() method.
Such as
- A title as title="My amazing title"
- xlabel as xlabel="x"
- ylabel as ylabel="y"
- the type of chart here wen have kind="bar" default is line
- etc...
Like so :
# We import the library
import pandas as pd
import matplotlib.pyplot as plt
# We create our dataframe
df = pd.DataFrame(index=range(0,10), data={"y1" : range(0,10)})
# We plot the dataframe
df.plot(title="My amazing title",
xlabel="X",
ylabel="y",
kind="bar")
plt.show()
Here you go ! You know how to make a basic bar plot in Python