2013-03-04 109 views
4

你能不能帮我找出如何绘制这种与matplotlib阴谋?如何绘制在一个图表多个横条与matplotlib

我具有表示表中的熊猫数据帧对象:

Graph  n   m 
<string> <int>  <int> 

我希望显示的nm大小为每个Graph:其中对于每个行中,有含有标签的水平条形图y轴左侧的Graph名称;在y轴的右边,有两个直接在另一个下面的细水平条,其长度代表nm。应该很清楚地看到,两个细条都属于标有图名的行。

这是迄今为止我所编写的代码:

fig = plt.figure() 
ax = gca() 
ax.set_xscale("log") 
labels = graphInfo["Graph"] 
nData = graphInfo["n"] 
mData = graphInfo["m"] 

xlocations = range(len(mData)) 
barh(xlocations, mData) 
barh(xlocations, nData) 

title("Graphs") 
gca().get_xaxis().tick_bottom() 
gca().get_yaxis().tick_left() 

plt.show() 

回答

8

这听起来像你想非常相似,这个例子的东西:http://matplotlib.org/examples/api/barchart_demo.html

作为开始:

import pandas 
import matplotlib.pyplot as plt 
import numpy as np 

df = pandas.DataFrame(dict(graph=['Item one', 'Item two', 'Item three'], 
          n=[3, 5, 2], m=[6, 1, 3])) 

ind = np.arange(len(df)) 
width = 0.4 

fig, ax = plt.subplots() 
ax.barh(ind, df.n, width, color='red', label='N') 
ax.barh(ind + width, df.m, width, color='green', label='M') 

ax.set(yticks=ind + width, yticklabels=df.graph, ylim=[2*width - 1, len(df)]) 
ax.legend() 

plt.show() 

enter image description here

相关问题