如何正确遍历matplotlib图形的子图以显示每行的最终y轴值? [英] How do I properly loop through the subplots of a matplotlib figure to show the final y-axis value of each line?

查看:181
本文介绍了如何正确遍历matplotlib图形的子图以显示每行的最终y轴值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前有一个matplotlib图形,具有16个子图,4列和4行.我生成了以下代码,这些代码循环遍历每个子图并在16个子图上绘制不同的数据:

I currently have a matplotlib figure with 16 subplots, 4 columns and 4 rows. I have the following code produced that loops through each subplot and plots different data on each of the 16 subplots:

fac_list = [one, two, three, four, five, six, seven, eight, nine, ten , eleven, twelve, 
thirteen, fourteen, fifteen, sixteen]
color = ['blue','red']

ds_i = 0
for row in range(0,subplot_shape[0]):
    for col in range(0,subplot_shape[1]):
        fac = fac_list[ds_i]
            plot('total', 
                    currentname=fac,
                    subplot_index=(row,col),
                    linestyle='-',
                    marker='.',
                    label='NEW',
                    color=color[0])
            leg2 = axes[row][col].legend(loc='upper left',bbox_to_anchor=(-0.10, 1.04), prop={'size':8},
                frameon=False, markerscale=0, handlelength=0)
            for line, text in zip(leg2.get_lines(), leg2.get_texts()):
                text.set_color(line.get_color())
            #axes[row][col].text(1.1,new_obj['total'].values[-1],new_obj['total'].values[-1],horizontalalignment='center',fontsize=5, rotation=45,
                #transform=axes[row][col].transAxes, color=color[0])
        ds_i += 1

目前,我已经知道了,因此注释行仅显示了fac_list中的最后一个项的最终y轴值(十六个),但是此值显示在图中的每个子图上,而不仅仅是最后一个子图(这是它应该显示在下面的子图).如何正确遍历子图,以使fac_list中每个项目的最终y轴值显示在每个对应的子图中(fac_list中的一个显示在子图(0,0)上,fac_list中的两个显示在子图上(0 ,1)等)?到目前为止,我没有尝试过任何方法.

Currently, I've got it so the commented out lines show the final y-axis value of only the last item in fac_list (sixteen), but this value shows up on every subplot in my figure, not just the last subplot (which is the subplot it should be showing up under). How do I loop through the subplots correctly so that the final y-axis value for each item in fac_list shows up in each corresponding subplot (one in fac_list shows up on subplot (0,0), two in fac_list shows up on subplot (0,1), etc.)? Nothing I've tried has worked so far.

推荐答案

取消注释该行,并将其置于与列的for循环相同的缩进级别

Uncomment the line and bring it at the same indentation level as the for loop for the column

for row in range(0,subplot_shape[0]):
    for col in range(0,subplot_shape[1]):
    ...
    ...

        for line, text in zip(leg2.get_lines(), leg2.get_texts()):
            text.set_color(line.get_color())

# Place the following line as it is in this answer
    axes[row][col].text(1.1,new_obj['total'].values[-1],new_obj['total'].values[-1],
                        horizontalalignment='center',fontsize=5, rotation=45,
                        transform=axes[row][col].transAxes, color=color[0])

这篇关于如何正确遍历matplotlib图形的子图以显示每行的最终y轴值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆