缩小matplotlib y轴值

时间:2017-05-10 09:12:54

标签: python matplotlib

我已经完成了以下代码(不要打扰常量,这些仅用于绘图):

percentage = [1.11, 1.63, 0.356, 0.808, 0.0459, 0.355, 0.133, 0.156, 0.0445, 0.631, 0.179, 0.226, 0.0272, 0.201, 0.177, 0.177, 0.224, 0.271, 0.176, 0.279, 0.302, 0.476, 0.397, 0.571, 0.491, 0.872, 1.08, 1.09, 1.23, 1.75, 1.96, 1.96, 1.68, 1.88, 1.57, 1.71, 1.09, 1.06, 1.05, 0.978, 0.724, 0.763, 0.691, 0.897, 0.817, 0.944, 0.825, 0.872, 0.911, 0.911, 0.895, 0.894, 0.823, 0.822, 0.838, 0.766, 0.766, 1.00, 1.01, 1.12, 1.14, 1.11, 1.57, 1.29, 1.69, 1.92, 1.99, 2.02, 2.04, 2.34, 2.45, 2.41, 2.44, 2.21, 2.13, 2.14, 1.89, 1.74, 1.53, 1.25, 1.31, 1.34, 1.38, 1.14, 1.00, 0.882, 0.826, 0.929, 0.580, 0.444, 0.293, 0.880, 0.618, 1.40, 0.538, 1.07]
result = dispatch_evs_arrival(1000, percentage)
samples = create_hist_value(result)
laws = pdf.create_distribution(2)

# Execute the algorithm
em.em_algorithm(samples, laws)
bins = []
i = 0
while i <= 96:
    bins.append(i*0.25)
    i = i + 1



matplotlib.rcParams.update({'font.size': 18})

# Plotting the graph.
plt.hist(samples, bins=bins, normed=1, color='r', alpha=0.5, histtype='bar', ec='black')
plt.xlabel("Time of day - 15 min. resolution")
plt.ylabel("Probability in %")
xmin, xmax = plt.xlim()
x = np.linspace(xmin, xmax, 10000)

plt.xlim(0, 25)
p = norm.pdf(x, laws[0].mean, laws[0].std_deviation)
p1 = norm.pdf(x, laws[1].mean, laws[1].std_deviation)

plt.plot(x, laws[0].weight * p + laws[1].weight * p1, 'k', linewidth=2)
plt.xticks(np.arange(0, 25, 2.0))
plt.yticks(np.arange(0, 0.13, 0.04))

#plt.plot(x, 'k', linewidth=2)

plt.grid()
plt.show()

这绘制如下图:

enter image description here

我想缩小y标签值除以4,这将给我整体相同的结果。是否可以使用matplotlib正确完成?

编辑:申请iCart答案给我这个:

enter image description here

这不是我想要的。我想要与第一个图中完全相同的结果,但是有0.03,0.02和0.01而不是0.12,0.08和0.04。我很确定它应该是可能的,因为整体形状不会改变。

以下是样本:

[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 
0.25, 0.25, 0.25, 0.25, 0.25, 0.25, 0.5, 0.5, 0.5, 0.75, 
0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 0.75, 1.25, 1.25, 1.25, 
1.5, 1.75, 1.75, 2.25, 2.25, 2.25, 2.25, 2.25, 2.25, 2.5, 
2.75, 2.75, 3.25, 3.25, 3.5, 3.75, 4.0, 4.0, 4.25, 4.25, 
4.5, 4.75, 4.75, 5.0, 5.0, 5.0, 5.25, 5.25, 5.25, 5.25, 
5.5, 5.5, 5.5, 5.75, 5.75, 5.75, 5.75, 5.75, 6.0, 6.0, 
6.0, 6.0, 6.25, 6.25, 6.25, 6.25, 6.25, 6.25, 6.25, 6.25, 
6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 6.5, 
6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 6.75, 
7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 7.0, 
7.0, 7.0, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 
7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.25, 7.5, 
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.75, 7.75, 
7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 
7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 7.75, 8.0, 8.0, 8.0, 
8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 8.0, 
8.0, 8.0, 8.0, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 
8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 8.25, 
8.25, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 
8.5, 8.5, 8.5, 8.5, 8.5, 8.5, 8.75, 8.75, 8.75, 8.75, 
8.75, 8.75, 8.75, 8.75, 8.75, 8.75, 8.75, 8.75, 8.75, 8.75, 
8.75, 8.75, 8.75, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 9.0, 
9.0, 9.0, 9.0, 9.25, 9.25, 9.25, 9.25, 9.25, 9.25, 9.25, 
9.25, 9.25, 9.25, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 9.5, 
9.5, 9.5, 9.5, 9.75, 9.75, 9.75, 9.75, 9.75, 9.75, 9.75, 
9.75, 9.75, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.0, 10.25, 
10.25, 10.25, 10.25, 10.25, 10.25, 10.25, 10.5, 10.5, 10.5, 10.5, 
10.5, 10.5, 10.75, 10.75, 10.75, 10.75, 10.75, 10.75, 10.75, 10.75, 
11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.0, 11.25, 11.25, 
11.25, 11.25, 11.25, 11.25, 11.25, 11.25, 11.25, 11.5, 11.5, 11.5, 
11.5, 11.5, 11.5, 11.5, 11.5, 11.75, 11.75, 11.75, 11.75, 11.75, 
11.75, 11.75, 11.75, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 12.0, 
12.0, 12.0, 12.25, 12.25, 12.25, 12.25, 12.25, 12.25, 12.25, 12.25, 
12.25, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.75, 
12.75, 12.75, 12.75, 12.75, 12.75, 12.75, 12.75, 13.0, 13.0, 13.0, 
13.0, 13.0, 13.0, 13.0, 13.0, 13.25, 13.25, 13.25, 13.25, 13.25, 
13.25, 13.25, 13.25, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 13.5, 
13.5, 13.75, 13.75, 13.75, 13.75, 13.75, 13.75, 13.75, 14.0, 14.0, 
14.0, 14.0, 14.0, 14.0, 14.0, 14.25, 14.25, 14.25, 14.25, 14.25, 
14.25, 14.25, 14.25, 14.25, 14.25, 14.5, 14.5, 14.5, 14.5, 14.5, 
14.5, 14.5, 14.5, 14.5, 14.5, 14.75, 14.75, 14.75, 14.75, 14.75, 
14.75, 14.75, 14.75, 14.75, 14.75, 14.75, 15.0, 15.0, 15.0, 15.0, 
15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.0, 15.25, 15.25, 15.25, 
15.25, 15.25, 15.25, 15.25, 15.25, 15.25, 15.25, 15.25, 15.5, 15.5, 
15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 15.5, 
15.5, 15.5, 15.5, 15.75, 15.75, 15.75, 15.75, 15.75, 15.75, 15.75, 
15.75, 15.75, 15.75, 15.75, 15.75, 16.0, 16.0, 16.0, 16.0, 16.0, 
16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 16.0, 
16.0, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 
16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 16.25, 
16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 
16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.5, 16.75, 
16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 
16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 16.75, 17.0, 
17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 
17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.0, 17.25, 
17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 
17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 17.25, 
17.25, 17.25, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.75, 17.75, 17.75, 17.75, 
17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 
17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 17.75, 
18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 
18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 18.0, 
18.0, 18.0, 18.0, 18.0, 18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 
18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 
18.25, 18.25, 18.25, 18.25, 18.25, 18.25, 18.5, 18.5, 18.5, 18.5, 
18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 
18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.5, 18.75, 18.75, 18.75, 
18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 
18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 18.75, 19.0, 19.0, 
19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 
19.0, 19.0, 19.0, 19.0, 19.0, 19.0, 19.25, 19.25, 19.25, 19.25, 
19.25, 19.25, 19.25, 19.25, 19.25, 19.25, 19.25, 19.25, 19.25, 19.25, 
19.25, 19.25, 19.25, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 
19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.5, 19.75, 19.75, 
19.75, 19.75, 19.75, 19.75, 19.75, 19.75, 19.75, 19.75, 19.75, 19.75, 
20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 20.0, 
20.0, 20.0, 20.0, 20.25, 20.25, 20.25, 20.25, 20.25, 20.25, 20.25, 
20.25, 20.25, 20.25, 20.25, 20.25, 20.25, 20.5, 20.5, 20.5, 20.5, 
20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.5, 20.75, 
20.75, 20.75, 20.75, 20.75, 20.75, 20.75, 20.75, 20.75, 20.75, 20.75, 
21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 21.0, 
21.25, 21.25, 21.25, 21.25, 21.25, 21.25, 21.25, 21.25, 21.5, 21.5, 
21.5, 21.5, 21.5, 21.5, 21.5, 21.5, 21.75, 21.75, 21.75, 21.75, 
21.75, 21.75, 21.75, 21.75, 21.75, 22.0, 22.0, 22.0, 22.0, 22.0, 
22.25, 22.25, 22.25, 22.25, 22.5, 22.5, 22.75, 22.75, 22.75, 22.75, 
22.75, 22.75, 22.75, 22.75, 23.0, 23.0, 23.0, 23.0, 23.0, 23.0, 
23.25, 23.25, 23.25, 23.25, 23.25, 23.25, 23.25, 23.25, 23.25, 23.25, 
23.25, 23.25, 23.25, 23.5, 23.5, 23.5, 23.5, 23.5, 23.75, 23.75,
23.75, 23.75, 23.75, 23.75, 23.75, 23.75, 23.75, 23.75]

缩放样本数组相同,但所有值除以4,这会导致x轴偏移。

2 个答案:

答案 0 :(得分:1)

plt.plot(x, (laws[0].weight * p + laws[1].weight * p1)/4, 'k', linewidth=2)
plt.xticks(np.arange(0, 25, 2.0))
plt.yticks(np.arange(0, 0.03, 0.01))

如果您在代码中更改这些行,并添加此

,结果如何
plt.ylim(0, 0.03)

答案 1 :(得分:0)

如果要缩小实际值,可以直接划分numpy数组:

In [1]: import numpy as np

In [2]: np.arange(0, 0.13, 0.04)
Out[2]: array([ 0.  ,  0.04,  0.08,  0.12])

In [3]: np.arange(0, 0.13, 0.04) / 4
Out[3]: array([ 0.  ,  0.01,  0.02,  0.03])