Python – matplotlib – creates a bar chart on the x-axis for each group on the y-axis

matplotlib – creates a bar chart on the x-axis for each group on the y-axis… here is a solution to the problem.

matplotlib – creates a bar chart on the x-axis for each group on the y-axis

I have the following data frame:

  data = pd. DataFrame(data={'group': ['A', 'B', 'C'],
 'start': [2, 5, 9], 'end': [9, 8, 6]})

I

would love to create the image below, I search everywhere
Can’t find the answer:

Charts

enter image description here

I’ve seen it everywhere, on the matplotlib website, on pandas,
I can’t find an easy way to do it.

Solution

You can draw horizontal bars using matplotlib >barh.

import pandas as pd
import matplotlib.pyplot as plt

data = pd. DataFrame(data={'group': ['A', 'B', 'C'],
                              'start': [2, 5, 6], 'end': [9, 8, 9]})

plt.barh(data["group"], data["end"]-data["start"], 0.4, data["start"], 
         color="None", edgecolor=["blue", "red", "green"])
plt.xlim(0,10)
plt.show()

enter image description here

Related Problems and Solutions