How to add x-axis lable in python bar chart

user12

hi i am trying to print python bar chart. here is my code.. and I also attached an image of my bar chart. the problems I am facing is that I want to write name of each category of bar chart on x-axis as CAT1, CAT2, CAT3, CAT4. right now its printing 0, 1, 2 on x-axis

and I also want to change the purple color of bar chart

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([['CAT1',9,3,24,46,76], ['CAT2', 48,90,42,56,68], ['CAT3', 31,24,28,11,90],
                   ['CAT4', 76,85,16,65,91]],
                  columns=['metric', 'A', 'B', 'C', 'D', 'E'])
                  
df.plot(
        kind='bar',
        stacked=False
        )

plt.legend(labels=['A', 'B', 'C', 'D', 'E'], ncol=4, loc='center', fontsize=15, bbox_to_anchor=(0.5, 1.06))

plt.show()

enter image description here

tyson.wu

By default, matplotlib recognizes the index of your dataframe as x-labels. I suggest you to add the following to make the column metric as the index, which allows matplotlib to automatically add label for you.

df = df.set_index('metric')

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to hide the y axis and x axis line and label in my bar chart for chart.js

How to create a bar chart with non-numeric X axis?

How to set X axis labels in MP Android Chart (Bar Graph)?

How to display x axis labels on all bar chart subplots?

How do I get a bar chart by pandas dataframe axis python

c3js: How to add a vertical line to a vertical bar chart that is not one of the categories of the x-axis?

Python Matplotlib – Bar chart on their representing sampling position on x-axis

How to resize bar chart based on no of x axis categories in HighCharts

Add secondary axis to stacked bar chart

How to add new x axis in chart JS?

how to create a bar chart in python with multiple x-axis

How can I add a second row of labels to the x axis of a matplotllib bar chart

how to add a second x-axis to a line chart in R

How to Arrange the x axis on a Plotly Bar Chart with R

R: x axis in bar chart - how widen the x-axis and center tick marks middle of bars

How to add a line on x-axis on a horizontal bar chart in d3

How to get selected bar x-axis name(Strings added to x-axis) using MP Chart?

How to align the x-lable with bar based on barplot() in R?

How to control the spacings of the x Axis label positions in a stacked bar chart?

How do I set a row as x axis in matplotlib bar chart?

How add months as a x-axis to stock chart/ high charts

How to add direct labels to a bar chart in ggplot for numeric x axis

How to prevent clipping of dual axis bar chart in python?

Scatter fusion chart - X Axis lable data is not getting displayed completely - PHP

How can I add double related x-axis bar to high chart in zoom

How to make a bar chart on range of values on x-axis and count on the range on y-axis in python?

How to remove decimal values on x-axis of bar chart : Highcharts

How to remove gaps in x axis in matplotlib bar chart?

How to nicely format the X-axis labels on a pandas bar chart