例如我有值
starTime1 = 1.5
starTime2 = 3
starTime3 = 2.3
我有三个水平的酒吧,例如名字: BAR1 BAR2 BAR3
我有“x”比例= 24小时(范围0.25)
那么如何从值“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 :(得分:0)
您可以使用left
关键字barh
(或垂直条的bottom
关键字)来更改条形的起始位置。但是,您必须从条形长度中减去相同的数量。
示例:
pylab.barh( [1,2,3], [10,20,30], left=20 )