Reputation: 11
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()
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
Reputation: 655
df['FuelType'].value_counts().sort_values().plot(kind = 'bar')
Upvotes: 2