Plotting barcode from Pandas data with dots

I want to plot a horizontal barcode from a Pandas frame but dont know how to start.

My data looks like

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

      

Couldn't help but paint something. I want to get something like this:

enter image description here

Colors are not important. Here is the data frame:

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"])

      

+3


source to share


4 answers


Here is a plot that is very similar to the picture from the question. It is created 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()

      

+2


source


You can make any drawing you want with matplotlib

.

You can specify the bottom / top of the horizontal hatches in matplotlib as it is done (using your dataframe as a reference):



import numpy as np
import matplotlib.pyplot as plt

# make a bar floating in space
index_labels = data.index
index = np.arange(index_labels)
right_edge = data['max']
left_edge = data['min']
plt.barh(index, right_edge, left=left_edge)
plt.yticks(index, index_labels)

      

This should generate a horizontal bar chart where you indicate the start and end points of the bars. You can link to the docs here: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.barh and see an example here: http://matplotlib.org/1.2.1/examples/pylab_examples/barh_demo.html ...

+1


source


I don't know your data, but pandas can display boxes from plane data by automatically grouping min, max and percentiles. One example close to yours might be the following:

import pandas as pd

pd.DataFrame({
    "Series 2":[20,25,20,30,45,60], 
    "Series 1":[10,12,20,40,45,50]
}).plot.box(vert=False)

      

enter image description here

0


source


If you really want draw points and hand-crafted rectangles, you can do it like this:

from matplotlib import pyplot as plt
from matplotlib.patches import Rectangle, Circle

fig = plt.figure()
ax = fig.add_subplot(111, xlim=(0, 70), ylim=(0, 70))
ax.add_artist(Rectangle(xy=(10, 49), color="g", width=40, height=2))
ax.add_artist(Circle(xy=(30, 50), color="orange", radius=1))
ax.add_artist(Circle(xy=(40, 50), color="red", radius=1))
ax.add_artist(Rectangle(xy=(20, 19), color="b", width=40, height=2))
ax.add_artist(Circle(xy=(40, 20), color="orange", radius=1))
ax.add_artist(Circle(xy=(50, 20), color="red", radius=1))
plt.show()

      

enter image description here

0


source







All Articles