Skip to content
Advertisement

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement