Pat Pat -4 years ago 197
Python Question

Plotting a barplot from Pandas dataframe with points

I want to plot a horizontal barplot from a Pandas dataframe but have no idea how to begin.

My data looks like this

max min point1 point2
Series 1 50 10 40 30
Series 2 60 20 50 40


Couldn't help myself but to draw something with paint. I want to end up with something like this:

enter image description here

The colors are not important. Here is the dataframe:

import pandas as pd

data = pd.DataFrame(dict(min=[10, 20],
max=[50, 60],
point1=[40, 50],
point2=[30, 40]),
index=["Series 1", "Series 2"])

Answer Source

Here is a plot that closely resembles to picture from the question. It's produced by matplotlib.pyplot.

enter image description here

import pandas as pd
import matplotlib.pyplot as plt


data = pd.DataFrame(dict(min=[10, 20],
                          max=[50, 60],
                          point1=[40, 50],
                          point2=[30, 40]),
                          index=["Series 1", "Series 2"])

plt.barh(range(len(data)), data["max"]-data["min"], height=0.3, left=data["min"])

plt.plot(data["point1"], range(len(data)), linestyle="", markersize=10, 
         marker="o", color="#ff6600", label="Point 1", markeredgecolor="k")
plt.plot(data["point2"], range(len(data)), linestyle="", markersize=10, 
         marker="o", color="#aa0000", label="Point 2", markeredgecolor="k")
plt.yticks(range(len(data)),data.index)

plt.ylim(-1,3)
plt.xlim(0,80)
plt.legend()
plt.show()
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download