使用点

时间:2017-03-23 21:19:51

标签: python pandas matplotlib

我想从Pandas数据框中绘制一个水平条形图,但不知道如何开始。

我的数据看起来像这样

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

无法帮助自己,只能用油漆画画。我想最终得到这样的东西:

enter image description here

颜色并不重要。这是数据框:

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

4 个答案:

答案 0 :(得分:2)

这是一个与问题图片非常相似的情节。它由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()

答案 1 :(得分:1)

您可以使用matplotlib完成所需的任何绘图。

您可以在matplotlib中指定水平条形图的底部/顶部,如此(使用您的数据框作为参考):

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)

这应生成一个水平条形图,您可以在其中指定条形的起点和终点。您可以在此处参考文档:http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.barh并在此处查看示例:http://matplotlib.org/1.2.1/examples/pylab_examples/barh_demo.html

答案 2 :(得分:0)

我不知道您的数据,但是大熊猫可以通过自动聚合最小值,最大值和百分位来绘制平面数据中的箱线图。一个例子,靠近你的可能就是这样:

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

答案 3 :(得分:0)

如果你真的需要一个手工制作的情节宽度点和矩形,你可以这样做:

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