From python-api #48:
Étienne:
The question now becomes if and/or how to automatically increase the margins to make annotations like theses fit in the figure's frame? Note that we could also increase the range (in this case, the x-axis' range) to make the annotations fit. But, this is probably not the best solution.
Andrew:
Well, i'll be getting the frame to go away there at least... it's extremely difficult to track down whether or not the ticks are set to be on or off so far. But i'm pretty sure i'll be able to track that down soon. In other words, we wouldn't need to increase the side of the data area if the frame wasn't showing in that plot.
Converting annotations from matplotlib to plotly is rather confusing. The following will hopefully shed some light on the topic.
import plotly
plotly.__version__
'1.1.1'
import plotly.plotly as py
import plotly.tools as tls
from plotly.graph_objs import *
import matplotlib.pyplot as plt
%matplotlib inline
data = Data([
Scatter(
x = [1,2,3],
y = [4,5,1]
)
])
layout= Layout(
xaxis= XAxis(
range=[0,4]
),
yaxis= YAxis(
range=[0,5.5]
),
annotations= Annotations([
Annotation(
xref='x1',
yref='y1',
x = 5,
y = 6,
text='out of range',
showarrow=False
),
Annotation(
xref='paper',
yref='paper',
x = 1.1,
y = 1.1,
text='ouside plot edge\n but still showing',
showarrow=False
)
])
)
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='mpl-annotations1')
So, the out of range annotation does not show when the plot is first rendered.
But, by zooming out (out of the set axis range) we can see it.
help(plt.text)
Help on function text in module matplotlib.pyplot: text(x, y, s, fontdict=None, withdash=False, **kwargs) Add text to the axes. Add text in string *s* to axis at location *x*, *y*, data coordinates. Parameters ---------- s : string text x, y : scalars data coordinates fontdict : dictionary, optional, default: None A dictionary to override the default text properties. If fontdict is None, the defaults are determined by your rc parameters. withdash : boolean, optional, default: False Creates a `~matplotlib.text.TextWithDash` instance instead of a `~matplotlib.text.Text` instance. Other parameters ---------------- kwargs : `~matplotlib.text.Text` properties. Other miscellaneous text parameters. Examples -------- Individual keyword arguments can be used to override any given parameter:: >>> text(x, y, s, fontsize=12) The default transform specifies that text is in data coords, alternatively, you can specify text in axis coords (0,0 is lower-left and 1,1 is upper-right). The example below places text in the center of the axes:: >>> text(0.5, 0.5,'matplotlib', horizontalalignment='center', ... verticalalignment='center', ... transform=ax.transAxes) You can put a rectangular box around the text instance (e.g., to set a background color) by using the keyword *bbox*. *bbox* is a dictionary of `~matplotlib.patches.Rectangle` properties. For example:: >>> text(x, y, s, bbox=dict(facecolor='red', alpha=0.5))
more info here
fig_mpl = plt.figure()
ax = plt.subplot(111)
plt.xlim(0, 4)
plt.ylim(0, 5.5)
plt.text(5,6,'out of range (but showing)',
horizontalalignment='center')
ax.annotate("ouside plot edge \n(axes fraction ref)",
xy=(1, 1), xycoords='data', # (useless w/o arrow)
xytext=(1.1,1.1), textcoords='axes fraction',
horizontalalignment='center',
#arrowprops=dict(facecolor='black', shrink=0.05) # arrow
)
plt.plot([1,2,3],[4,5,1])
[<matplotlib.lines.Line2D at 0x7f22f776e0d0>]
data = Data([
Scatter(
x = [1,2,3],
y = [4,5,1]
)
])
layout= Layout(
xaxis= XAxis(
range=[0,4]
),
yaxis= YAxis(
range=[0,5.5]
),
annotations= Annotations([
Annotation(
xref='x1',
yref='y1',
x = 3.5,
y = 3,
text='A loooooooooooooooooooooooooooooooonnnnnnnngggggggg annotation',
showarrow=False,
xanchor='left'
),
]),
showlegend=False
)
fig = Figure(data=data, layout=layout)
py.iplot(fig, filename='mpl-annotations2')
fig_mpl = plt.figure()
ax = plt.subplot(111)
plt.xlim(0, 4)
plt.ylim(0, 5.5)
plt.text(3.5,3,'A loooooooooooooooooooooooooooooooonnnnnnnngggggggg annotation')
plt.plot([1,2,3],[4,5,1])
[<matplotlib.lines.Line2D at 0x7f22d27377d0>]
from IPython.display import display, HTML
import urllib2
url = 'https://raw.githubusercontent.com/plotly/python-user-guide/master/custom.css'
display(HTML(urllib2.urlopen(url).read()))