Rename labels of a bar chart Matplotlib - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: Rename labels of a bar chart Matplotlib (/thread-27950.html) |
Rename labels of a bar chart Matplotlib - smalatray - Jun-28-2020 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 RE: Rename labels of a bar chart Matplotlib - hussainmujtaba - Jul-01-2020 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 |