Home > Enterprise >  plot textboxes and fill colors between vertical lines in matplotlib python
plot textboxes and fill colors between vertical lines in matplotlib python

Time:05-05

based on another enter image description here

As you can see the blue vertical lines indicate borders set by multiples of standard deviations. I would like to add the following information and color coding, which I now quickly placed in powerpoint:

enter image description here

I tried to mess with the plt.fill_between function but didnt really get anything useful. Also I do not know how to write something, like the mu l*sigma here, above the plot. How can i achieve the second picture based on what I have?

EDIT: solved by @Trenton McKinney

enter image description here Putting new boxes inside the colored boxes:

for i, (x, c) in enumerate(locs[:-1]):
            axes.axvspan(x, locs[i   1][0], alpha=0.2, color=c)
            tx = (x   locs[i   1][0]) / 2
            axes.text(tx, y1/2, f'Zustand {i   1}', {'ha': 'center', 'va': 'center'}, rotation=90)
            if i<4:
                axes.text(tx, y1/1.25, r"$\mu$"   "-"   f"{4-i}"  "$\cdot$"   "$\sigma$" , {'ha': 'center', 'va': 'center'}, rotation=90, bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))
            else:
                axes.text(tx, y1/1.25, r"$\mu$"   " "   f"{i-4   1}"  "$\cdot$"   "$\sigma$" , {'ha': 'center', 'va': 'center'}, rotation=90, bbox=dict(facecolor='white', alpha=0.8, edgecolor='black'))

CodePudding user response:

  • It will be easier to create a container with all of the values for the vertical lines because those values will be reused for placing the lines, and determining the axvspan and text placement. In this case, a dictionary is used.
  • See inline notation for explanations
  • Use enter image description here

    Update for additional annotations

            # extra annotations
            sign = [f'µ - {v}σ' for v in range(4, 0, -1)]
            sigp = [f'µ   {v}σ' for v in range(1, 5)]
            anno = sign   sigp
            
            # iterate through all but the last value and add the vspan and the text
            for i, (x, c) in enumerate(locs[:-1]):
                axes.axvspan(x, locs[i   1][0], alpha=0.2, color=c)
                tx = (x   locs[i   1][0]) / 2
                axes.text(tx, y1/2, f'Stage {i   1}: {anno[i]}', {'ha': 'center', 'va': 'center'}, rotation=90)
    

    enter image description here

  • Related