2015-11-16 68 views
0

我已经构建了一个水平条形图,如下面的代码所示。 一切看起来不错,除了应该注释的酒吧。 我不知道如何获得酒吧的结束标签,而不是在图表中间的表格。具体来说,我期待在这行代码:MatPlotLib Python 3.4:枚举和ax.annotate

for i, text in enumerate(ind): 
    ax.annotate(str(x_axis), (ind[i], x_axis[i]),fontsize=14, color='black', va='center', ha='center') 

这里的整个事情:

import pandas as pd 
import numpy as np 
from matplotlib import pyplot as plt 
%matplotlib inline 
df0=pd.read_csv('sample1.csv') 


fig, ax = plt.subplots(1, 1, figsize=(12,9)) 

ax.spines['top'].set_visible(False) 
ax.spines['bottom'].set_visible(False) 
ax.spines['right'].set_visible(False) 
ax.spines['left'].set_visible(False) 

x_axis = 8.6667, 9.5, -8.4, 0, -4, 6 
y_axis = Hospital 
y_axis = 'A','B','C','D','E','F' 
ind = np.arange(len(y_axis)) 

plt.xticks(fontsize=12) 
plt.yticks(ind, y_axis, fontsize=14) 
plt.tick_params(
axis='x', 
which='both', 
top='off') 

plt.tick_params(
axis='y', 
which='both', 
right='off', 
left='off') 
plt.title('Super Awesome Title', fontsize=18, ha='center') 

#Label Bars: First is the older, simpler way but it has issues... 
#for a, b in zip(ind, y_axis): 
# plt.text(a, b, str(b), fontsize=16, color='black') 
#Better, but more complex method:  
for i, text in enumerate(ind): 
    ax.annotate(str(x_axis), (ind[i], x_axis[i]),fontsize=14, color='black', va='center', ha='center') 

#Add padding around x_axis 
plt.xlim([min(x_axis)-.5, max(x_axis)+.5]) 
#Add padding around y_axis 
plt.ylim([min(ind)-.5, max(ind)+.5]) 

plt.barh(ind, x_axis, align='center', height=0.5, edgecolor='none', color='#AAA38E') 

回答

1

我想通了:

,我必须改IND和X_AXIS四周,遍历X_AXIS:

ax.annotate(str(x_axis), (ind[i], x_axis[i]) 

应该是:

ax.annotate(str(x_axis[i]), (x_axis[i], ind[i]) 
+0

记得接受你自己的答案。 – tacaswell