12345
12345

Reputation: 11

Barplot in Python

Do we need to specify the frequency of a variable in our code to plot the barplot using Python? Can we plot a barplot for a categorical variable column in a data frame, without specifying frequency using Python? Example code:

counts = [968,116,12] #Here I specified frequency of variable in a dataframe
fuelType = ('Petrol', 'Diesel', 'CNG')
index = np.arange(len(fuelType))
plt.bar(index, counts, color=['red', 'blue', 'green'])
plt.title("Bar plot of fuel types")
plt.xlabel('Fuel Types')
plt.ylabel('Frequancy')
plt.xticks(index, fuelType, rotation = 90) 
plt.show()

Link for Dataframe

In the first line of code, I specified the frequency of the variable in the data frame. My question is can we plot barplot without specifying that frequency, instead of making it find frequency by its own form data frame and plot a barplot?

Upvotes: 1

Views: 160

Answers (1)

Rishin Rahim
Rishin Rahim

Reputation: 655

df['FuelType'].value_counts().sort_values().plot(kind = 'bar')

enter image description here

Upvotes: 2

Related Questions