2014-11-03 50 views
0

,比如我有值情節從開始時間算起不棒從零

starTime1 = 1.5 
starTime2 = 3 
starTime3 = 2.3 

,我有三個酒吧horizantal,例如用名: BAR1 BAR2 BAR3

我有「× 「scale = 24小時(範圍0.24)

那麼如何從值」startTime「而不是從零繪製條形圖。

有我的代碼。變量「排名」是酒吧的結束時間。

import csv 
import datetime 
import time 
import numpy as np 
import matplotlib.pyplot as plt 
import pylab 
from matplotlib.ticker import MaxNLocator 

numCp = 10 
cpNames = [data[4][6], data[5][6], data[6][6], data[7][6], 
     data[8][6], data[9][6], data[10][6], data[11][6], 
     data[12][6], data[13][6]] 
testMeta = ['UOL/sec' , 'UOL/sec', 'UOL/sec', 'UOL/sec', 'UOL/sec', 'UOL/sec',  'UOL/sec', 'UOL/sec', 'UOL/sec', 'UOL/sec' ] 
scores = [data[4][4], data[5][4], data[6][4], data[7][4], data[8][4], 
     data[9][4], data[10][4], data[11][4], data[12][4], data[13][4]] 
rankings = intAllCpMin 



fig, ax1 = plt.subplots(figsize=(9, 7)) #height and width of chart 
plt.subplots_adjust(left=0.115, right=0.88) 
fig.canvas.set_window_title('Eldorado K-8 Fitness Chart') 
pos = np.arange(numCp)+0.5 # Center bars on the Y-axis ticks 
rects = ax1.barh(pos, rankings, align='center', height=0.5, color='m') 

ax1.axis([0, 100, 0, 10]) # to display all CP bars correctly change last number on  numbers of CP 
pylab.yticks(pos, cpNames) 
ax1.set_title('Non Functional Test Results') 
plt.text(50, -0.5, 'Hours', 
    horizontalalignment='center', size='small') 

ax2 = ax1.twinx() 
ax2.plot([totalRunTime, totalRunTime], [0, 5], 'white', alpha=0.1) #width of X 
ax2.xaxis.set_major_locator(MaxNLocator(totalRunTime)) 
xticks = pylab.setp(ax2, xticklabels=[lowestStartTime," "," "," "," "," ",  higherStartTime]) 
ax2.xaxis.grid(True, linestyle='--', which='major', color='grey', 
alpha=0.25) 

def withnew(i, scr): 
    if testMeta[i] != '': 
     return '%s\n' % scr 
    else: 
     return scr 

scoreLabels = [withnew(i, scr) for i, scr in enumerate(scores)] 
scoreLabels = [i+j for i, j in zip(scoreLabels, testMeta)] 
ax2.set_yticks(pos) 
ax2.set_yticklabels(scoreLabels) 
ax2.set_ylim(ax1.get_ylim()) 


ax2.set_ylabel('Test Scores') 

suffixes = sorted([str(data[4][14][5:12]), data[5][14][5:12], data[6][14][5:12], data[7][14][5:12], data[8][14][5:12], 
       data[9][14][5:12],data[10][14][5:12], data[11][14][5:12], data[12][14] [5:12], data[13][14][5:12]]) 


    width = int(rect.get_width()) 

    rankStr = "" 
    if (width < 2):  # The bars aren't wide enough to print the ranking inside 
     xloc = width+0.1 # Shift the text to the right side of the right edge 
     clr = 'black'  # Black against white background 
     align = 'left' 
     rankStr += suffixes[1] + " Total Run Time" 
    else: 
     xloc = 0.50*width # Shift the text to the left side of the right edge 
     clr = 'white'  # White on magenta 
     align = 'center' 
     rankStr += suffixes[1] + " Total Run Time" 

     yloc = rect.get_y()+rect.get_height()/2.0 
     ax1.text(xloc, yloc, suffixes[1], horizontalalignment=align, 
     verticalalignment='center', color=clr, weight='bold') 


plt.show() 
+0

這不運行。數據丟失。 – freshtop 2014-11-03 22:31:02

回答

0

可以使用left關鍵字barh(或bottom關鍵字的豎線)更改欄的起始位置。儘管如此,你必須從長度減去相同的數量。

實施例:

pylab.barh([1,2,3], [10,20,30], left=20) 
+0

非常感謝! – 2014-11-04 10:55:17