简体   繁体   English

如何正确遍历 matplotlib 图的子图以显示每条线的最终 y 轴值?

[英]How do I properly loop through the subplots of a matplotlib figure to show the final y-axis value of each line?

I currently have a matplotlib figure with 16 subplots, 4 columns and 4 rows.我目前有一个带有 16 个子图、4 列和 4 行的 matplotlib 图。 I have the following code produced that loops through each subplot and plots different data on each of the 16 subplots:我生成了以下代码,该代码循环遍历每个子图并在 16 个子图中的每一个上绘制不同的数据:

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

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).目前,我已经知道了,因此注释掉的行仅显示 fac_list (十六)中最后一项的最终 y 轴值,但该值显示在我图中的每个子图上,而不仅仅是最后一个子图(即它应该显示在下面的子图)。 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.)?如何正确循环子图,以便 fac_list 中每个项目的最终 y 轴值显示在每个相应的子图中(fac_list 中的一个显示在子图(0,0)上,fac_list 中的两个显示在子图(0 ,1)等)? Nothing I've tried has worked so far.到目前为止,我尝试过的任何方法都没有奏效。

Uncomment the line and bring it at the same indentation level as the for loop for the column取消注释该行并将其置于与列的 for 循环相同的缩进级别

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM