1

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?

8
  • Please provide example of data and desired output Commented Apr 16, 2021 at 9:24
  • I think you need a histogram, not a barplot... Commented Apr 16, 2021 at 9:43
  • @Daweo now updated my question, can you help me? Commented Apr 16, 2021 at 9:43
  • @Djib2011, my question is can we do barplot without specifying frequency? Commented Apr 16, 2021 at 9:44
  • Can you please add the original dataframe? Commented Apr 16, 2021 at 9:44

1 Answer 1

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

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Please select this answer as the accepted answer and upvote if possible.
I will try to compensate the "not-acceptance" of the answer with an upvote ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.