Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Create annotation space above the graph in Plotly

I would like to create additional space for annotations within the plot (see green area in attached image). At the moment, the y-axis defines my height of the plot. Can I push the plot beyond the y.max limit/hide the y-axis after a certain point (marked red in the image)? I try to avoide the axis reaching into the 'comment section' (green).

Thank you!

enter image description here

enter image description here

like image 241
Dana_Miles Avatar asked Jul 05 '26 19:07

Dana_Miles


1 Answers

I stumbled on this question after answering another of your questions and thought it was interesting.

Is this what you're after?

Key Elements:

The key elements of creating the elevated annotation are:

  • xref and yref are set to 'paper', thus using the graph's background for xy coordinates rather than graph data values
  • Using > 1.00 for the y positioning
  • layout['margin'] top set to a value to create a top margin, thus pushing the graph area down

Sample Code:

It's really much simpler than it looks.

import numpy as np
from plotly.offline import iplot

# Create the dataset.
n = 360
x = np.arange(n)
y = [np.sin(i*(np.pi/180)) for i in range(n)]

# Plot the sin wave.
data = []
data.append({'x': x,
             'y': y})

# Create the annotations.
notes = []
notes.append({'text': ('An annotation at the top of the graph.<br><br>'
                       'And some more rambling text to take up some '
                       'more space on the graph to represent<br>'
                       'a longer annotation, just because we can.<br><br>'
                       'And one more for good measure.'),
              'x': 0.00,
              'y': 1.60,
              'xref': 'paper',
              'yref': 'paper',
              'showarrow': False,
              'align': 'left'})
notes.append({'text': 'Elevated Graph Annotation Example',
              'x': 0.00,
              'y': 1.15,
              'xref': 'paper',
              'yref': 'paper', 
              'showarrow': False,
              'font': {'size': 18}})

# Setup the layout.
layout = {}
layout['annotations'] = notes
layout['margin'] = {'t': 175}

# Plot the data. (May also use `plot` rather than `iplot`)
iplot({'data': data, 'layout': layout})

Note:

You'll have to create your own graph title (rather than relying on the title parameter available in layout) as you'll need to position the graph title yourself. Otherwise the automatic title and annotations will overlap.

like image 142
S3DEV Avatar answered Jul 07 '26 08:07

S3DEV



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!