Python Forum
Rename labels of a bar chart Matplotlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename labels of a bar chart Matplotlib
#1
Hi,

I have started Python programming classes last month to become a data analyst.
I have been blocked for a while with the following issue: how do I rename/replace the labels on the X axis of my bar chart in Matplotlib?

Here the link to the data I have been using: Data for bar chart

Right now I get numbers as labels (1,5,3,0,4,2)for the x axis.
I would like to replace / rename them with text of the first column of my data (labels should be: Data Analysis, Machine Learning, Data Visualization, Big Data, Deep Learning, Data journalist).

Here my code:

df_interested = pd.read_csv('https://cocl.us/datascience_survey_data')
interested = df_interested.sort_values(['Very interested'], ascending = False, axis = 0, inplace = True)
df_interested.plot(kind = 'bar', figsize = (20,8), width = 0.8, color=['#5cb85c', '#5bc0de', '#d9534f'])
plt.title('Pourcentage of respondents interest in Data Science Areas', size = 16)
plt.legend(prop={"size":14})

sns.despine()
sns.despine(top=True, right=True, left=True, bottom=False)

plt.xlabel("X Label")
plt.ylabel("Y Label")
ax = plt.gca()
ax.axes.xaxis.set_visible(False)
ax.axes.yaxis.set_visible(False)
plt.grid(True)

plt.show()
The bar chart gets displayed the way I want. I am just struggling with the X axis labeling...
Thanks in advance for your huge help.

Stan
Reply
#2
You need to set the index as these fields as shown below:
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

df_interested = pd.read_csv(r'C:\Users\mirza\Downloads\Topic_Survey_Assignment.csv',index_col="Unnamed: 0")
interested = df_interested.sort_values(['Very interested'], ascending = False, axis = 0, inplace = True)
df_interested.plot(kind = 'bar', figsize = (20,8), width = 0.8, color=['#5cb85c', '#5bc0de', '#d9534f'])
plt.title('Pourcentage of respondents interest in Data Science Areas', size = 16)
plt.legend(prop={"size":14})
 
sns.despine()
sns.despine(top=True, right=True, left=True, bottom=False)
 
plt.xlabel("X Label")
plt.ylabel("Y Label")
ax = plt.gca()
# ax.axes.xaxis.set_visible(False)
# ax.axes.yaxis.set_visible(False)
plt.grid(True)
 
plt.show()
To know about data visualization and pandas, have a look at these articles
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question I’m trying to create a Power BI Matplotlib quadrant chart and I’m a little stumped. Nidwolff 1 323 Mar-04-2024, 06:07 AM
Last Post: Danishhafeez
  Matplotlib bar chart ollarch 0 1,372 Mar-04-2020, 10:45 AM
Last Post: ollarch
  Spacing pie chart colours evenly in matplotlib? Giovanni_diJacopo 1 3,229 Jul-12-2019, 12:31 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020