[英]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. 我想从Pandas数据框中绘制一个水平条形图,但不知道如何开始。
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: 我想最终得到这样的东西:
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"])
Here is a plot that closely resembles to picture from the question. 这是一个与问题图片非常相似的情节。 It's produced by matplotlib.pyplot
. 它由matplotlib.pyplot
。
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()
You can do any drawing you need with matplotlib
. 您可以使用matplotlib
进行任何绘图。
You can specify the bottoms / tops of horizontal barplots in matplotlib like so (using your dataframe as a reference): 您可以在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)
That should generate a horizontal bar plot where you specify the starting and ending points of the bars. 这应生成一个水平条形图,您可以在其中指定条形的起点和终点。 You can reference 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 . 您可以在此处参考文档: http : //matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.barh并在此处查看示例: http : //matplotlib.org/1.2.1/examples/pylab_examples/barh_demo。 HTML 。
I do not know your data, but pandas can plot boxplots from the plane data by aggregating min, max and percentiles automatically. 我不知道您的数据,但是大熊猫可以通过自动聚合最小值,最大值和百分位来绘制平面数据中的箱线图。 One example, close to yours might be this: 一个例子,靠近你的可能是这样的:
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)
If you realy need a hand-crafted plots width dots and rectangles you may do this like here: 如果你真的需要一个手工制作的情节宽度点和矩形,你可以这样做:
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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.