Let’s look at an example first. Let’s read the “titanic” dataset. The dataset contains various information, such as embark town, age of passengers, whether the passenger survived, etc. Let’s say we want to know how many passengers embarked from which town. To know that, we can use a bar chart.
import pandas from matplotlib import pyplot df = pandas.read_csv("titanic.csv") print(df.info()) series1 = df.embark_town.value_counts() print(series1.index) print(series1.values) pyplot.bar(series1.index, series1.values) pyplot.xlabel("Embark Town") pyplot.ylabel("Number of Passengers") pyplot.savefig("matplotlib-bar-chart.png") pyplot.close()
In the Python code above, df.embark_town.value_counts() gives us a series that contains information on how many passengers embarked from each embark town (How to find the count of unique records for all the unique values in a column of a DataFrame?).
We are using the pyplot.bar() function to plot the bar chart. Here, series1.index gives us the names of the embark town. And series1.values gives us the number of passengers from each of those embark towns. We are passing these values as two parameters of the pyplot.bar() function. We are also labeling the x-axis and the y-axis using the pyplot.xlabel() and pyplot.ylabel() functions, respectively.
The resulting bar chart will look like the following:






0 Comments