2013-03-04 69 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

相關問題