Fre*_*ord 2 python matplotlib bar-chart
我有一些数据想用 mathplotlib 绘制。我有一个基准的测量值,我想与参考值进行比较。我计算了一个“减速因子”来表明一个浏览器与另一个浏览器相比慢了多少。
到目前为止,它看起来几乎是这样的:

情节的代码是:
#!/usr/bin/env python
import numpy as np
# Import Standard error of the mean
from scipy import mean
from scipy.stats import sem
Y_LABEL = "Slowdown factor"
X_LABEL = "Browser"
import matplotlib as mpl
import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(111)
sampledata={}
for browser in ('firefox',
'chrome',
'internet-explorer'
):
sampledata[browser] = {}
for benchmark in ('data1', 'data2', 'data3'):
sampledata[browser][benchmark] = {}
sampledata['firefox']['data1'] = [10,5,20,10,15]
sampledata['chrome']['data1'] = [5,7,9,10,11]
sampledata['internet-explorer']['data1'] = [20,30,40,20,30]
sampledata['firefox']['data2'] = [10,50,20,10,14]
sampledata['chrome']['data2'] = [50,70,90,100,80]
sampledata['internet-explorer']['data2'] = [200,300,400,300,300]
sampledata['firefox']['data3'] = [90,50,100,100,140]
sampledata['chrome']['data3'] = [50,170,90,100,80]
sampledata['internet-explorer']['data3'] = [200,200,100,100,300]
data = {}
for browser in ('firefox',
'internet-explorer'
):
data[browser] = {}
for benchmark in ('data1', 'data2', 'data3',):
data[browser][benchmark] = sampledata[browser][benchmark]
baselinedata = sampledata['chrome']
## the data
chrome_vanillas = [results_for_benchmark
for results_for_benchmark in baselinedata.itervalues()]
chrome_vanilla_means = [mean(v) for v in chrome_vanillas]
chrome_vanilla_errors = [sem(v) for v in chrome_vanillas]
baseline_values = chrome_vanillas
baseline_means = chrome_vanilla_means
firefoxes = [results_for_benchmark
for results_for_benchmark in data['firefox'].itervalues()]
firefoxes = [[float(v)/bl
for (v, bl) in zip(v_l, bl_l)]
for (v_l, bl_l) in zip(firefoxes, baseline_values)]
firefox_means = [mean(v) for v in firefoxes]
firefox_errors = [sem(v) for v in firefoxes]
internet_explorers = [results_for_benchmark
for results_for_benchmark in data['internet-explorer'].itervalues()]
internet_explorers = [[float(v)/bl
for (v, bl) in zip(v_l, bl_l)]
for (v_l, bl_l) in zip(internet_explorers, baseline_values)]
internet_explorer_means = [mean(v) for v in internet_explorers]
internet_explorer_errors = [sem(v) for v in internet_explorers]
N = min(len(browser) for browser in data.itervalues())
ind = np.arange(N) # the x locations for the groups
width = 0.25 # the width of the bars
# axes and labels
#ax.set_xlim(-width,len(ind)+width)
#ax.set_ylim(-45,45)
ax.set_ylabel(Y_LABEL)
ax.set_title(X_LABEL)
## the bars
firefox_rects = ax.bar(ind, firefox_means, width,
color='green',
yerr=firefox_errors,
error_kw=dict(elinewidth=2,ecolor='black'))
internet_explorer_rects = ax.bar(ind+width, internet_explorer_means, width,
color='blue',
yerr=internet_explorer_errors,
error_kw=dict(elinewidth=2,ecolor='black'))
xTickMarks = [key
for key in data.itervalues().next().keys()]
ax.set_xticks(ind+width)
xtickNames = ax.set_xticklabels(xTickMarks)
plt.setp(xtickNames, rotation=45, fontsize=10)
## add a legend
ax.legend( (firefox_rects[0], internet_explorer_rects[0]),
('Firefox', 'Internet Explorer') )
plt.savefig('figure.png')
plt.show()
Run Code Online (Sandbox Code Playgroud)
现在,我想将基线设置为1.0,如果值小于 1,则让条形变小。我已经看到了函数的bottom参数bar,但它似乎只是为我拥有的每个值加 1,而不是考虑 1 到成为绘制条形的基础。
Matlab 似乎可以很容易地做到这一点,使用类似的东西set(hBars(1),'BaseValue',2);:

所以最终的问题是:如何创建一个基线为 1.0 并让条形变长的图?
您可以结合使用格式化程序(用于刻度)并为您的数据减 1。
例子:
import matplotlib.pyplot as plt
import matplotlib.ticker as mtick
baseline = 1
data = [1.1,2.0,1.4,0.9,1.6,0.7,0.1]
plt.bar(range(len(data)),[x-baseline for x in data])
plt.gca().yaxis.set_major_formatter(mtick.FuncFormatter(lambda x,_: x+baseline))
plt.show()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3403 次 |
| 最近记录: |