matplotlib生成多个数字,1个空白,1个错误,fencepost错误?

时间:2014-09-26 17:23:10

标签: python matplotlib

我正在生成五个数字,四个折线图和一个散点图。

如果我单独生成它们,它们一切都很好。如果我一起生成它们,我最后得到的是我的第一个数字空白,图2-4是“可操作的” - >'组件',并正确绘制。 然而,图5中有'标签'折线图,标题和'时间'散点图中的标签 - 并且没有实际的散点图。

我做了什么围栏或其他恐怖事件?

fn = 0
pltkw = {
    'actionable': {},
    'causes': {},
    'components': {},
    'labels': {},
}

for figure in ['actionable', 'causes', 'components', 'labels']:
    fn += 1
    fig[figure] = plt.figure(fn, figsize=(18,10))
    frm[figure] = pd.DataFrame(data[figure], index=date_range)
    axs[figure] = frm[figure].plot(**pltkw[figure])
    txs[figure] = plt.title(figure)
    yls[figure] = plt.ylabel('events')

fn += 1
figure = 'times'
fig[figure] = plt.figure(fn, figsize=(18,10))
frm[figure] = pd.DataFrame(data[figure])
axs[figure] = plt.scatter(frm[figure]['hour'], frm[figure]['day'])
txs[figure] = plt.title(figure)
xls[figure] = plt.xlabel('hour 0-23')
yls[figure] = plt.ylabel('mon,tue,wed,thu,fri,sat,sun')

plt.show()

1 个答案:

答案 0 :(得分:1)

fn = 0
pltkw = {
    'actionable': {},
    'causes': {},
    'components': {},
    'labels': {},
}

for figure in ['actionable', 'causes', 'components', 'labels']:
    # get an axes and figure 
    fig_, ax = plt.subplots(1, 1, figsize=(18, 10)) 
    # I assume you don't _really_ care about the figure numeber, but are using this
    # to get around the global state

    fig[figure] = fig_ 
    axs[figure] = ax
    frm[figure] = pd.DataFrame(data[figure], index=date_range)
    frm[figure].plot(ax=ax, **pltkw[figure])
    # pandas really should return the artists added, but oh well
    txs[figure] = ax.set_title(figure)
    yls[figure] = ax.set_ylabel('events')


figure = 'times'
fig_, ax = plt.subplots(1, 1, figsize=(18, 10))
frm[figure] = pd.DataFrame(data[figure])
axs[figure] = ax
ax.scatter(frm[figure]['hour'], frm[figure]['day'])
txs[figure] = ax.set_title(figure)
xls[figure] = plt.set_xlabel('hour 0-23')
# not sure this is really doing what you want, but ok
yls[figure] = plt.set_ylabel('mon,tue,wed,thu,fri,sat,sun')

plt.show()