在子图中强制ylim范围

时间:2017-07-19 17:19:02

标签: python-2.7 matplotlib

在使用matplotlib绘制一系列子图时,我无法正确设置ylim范围。


这是代码的一部分:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl

(...) # loading npy data 

titles = ["basestr1", "basestr2", "basestr3", "basestr4", "basestr5"]
labels = ["baselab1", "baselab2", "baselab3", "baselab4", "baselab5"]
linew = 2.24
ms    = 10
mw    = 2
fc    = (1,1,1)
bc    = (1,1,1)
mpl.rcParams['axes.prop_cycle'] = mpl.cycler(color=[(1,0.4,0.4), (0.1,0.6,0.1), (0.04,0.2,0.04)]) 
mpl.rcParams.update({'font.size': 12})

fig2 = plt.subplots(2, 2, figsize=(12,9), facecolor=fc)
plt.rc('font', family='serif')

ax0 = plt.subplot(221)
ax1 = plt.subplot(222)
ax2 = plt.subplot(223)
ax3 = plt.subplot(224)
axl = [ax0, ax1, ax2, ax3]

em = []
fp = []
fn = []
gm = []

for c,element in enumerate(elements):
    em.append([i[0] for i in element])
    fp.append([i[1][1] if 1 in i[1] else 0 for i in element]) # red
    fn.append([i[1][2] if 2 in i[1] else 0 for i in element]) # light green
    gm.append([i[1][3] if 3 in i[1] else 0 for i in element]) # dark green

    axl[c].semilogy(em[c], fp[c], "-x", lw=linew, markersize=ms, mew=mw) # red
    axl[c].semilogy(em[c], fn[c], "-x", lw=linew, markersize=ms, mew=mw) # light green
    axl[c].semilogy(em[c], gm[c], "-o", lw=linew, markersize=ms, mew=mw, mfc='None') # dark green
    axl[c].set_ylim([-10, 200]) # <-- Here's the issue; it seems not to work properly.
    axl[c].grid(True,which="both")
    axl[c].set_title(titles[c])
    axl[c].set_xlabel(labels[c])
    axl[c].set_ylabel(r'Count')


plt.legend(['False', 'True', 'Others'], loc=3, bbox_to_anchor=(.62, 0.4), borderaxespad=0.)
plt.tight_layout(pad=0.4, w_pad=0.5, h_pad=1.0)

plt.savefig('/home/username/Desktop/figure.png',
            facecolor=fig2.get_facecolor(),edgecolor='w',orientation='landscape',papertype=None,
            format=None, transparent=False, bbox_inches=None, pad_inches=0.1,
            frameon=None)
plt.show() # block=False

其中elements是包含4个数组的列表 这些数组中的每一个都如下所示:

elements[0]
Out[16]: 
array([[1, {0.0: 1252, 1.0: 11, 2.0: 170, 3.0: 11}],
       [2, {0.0: 1251, 1.0: 12, 2.0: 163, 3.0: 18}],
       [3, {0.0: 1229, 1.0: 34, 2.0: 148, 3.0: 33}],
       ..., 
       [6, {0.0: 1164, 1.0: 99, 2.0: 125, 3.0: 56}],
       [7, {0.0: 1111, 1.0: 152, 2.0: 105, 3.0: 76}],
       [8, {0.0: 1056, 1.0: 207, 2.0: 81, 3.0: 100}]], dtype=object)

我哪里错了?
我可以在axl[c].set_ylim([-10, 200])中设置我想要的任何值,它不会改变输出图上的任何内容。

更新

好的,似乎无法将其他值设置为1作为此处的起始y轴值。

0 个答案:

没有答案