Leading/trailing spaces ignored in annotations, only in SVG output

I position row total annotations at the end of horizontal bar plots, using the row total as x position value and left-aligning the string. I’d like to keep a gap, so the numbers stand a bit apart from the bars for legibility. This is possible by prepending 1-2 spaces to the annotation. When using fig.show() or pio.show(fig, "svg"), it displays as expected.

However, when using pio.write_image(fig, filename), leading spaces in the annotation string are missing.

And to add, I surround the annotation string with <b>...</b> and spaces both before and after the initial <b> are ignored.

Any tricks to circumvent this, or a place to report this? It seems like undesired behavior.

Cheers

Hey @andom, does the ticksuffix help?

Unfortunately not, as I am using fig.add_annotation(), not tick labelling for these totals. Let me show you…

fig.show() & pio.show(fig, "svg"):

pio.write_image(fig, "filename.svg"):

Note also the slight spacing differences in the within-bar annotations (which aren’t my problem here though).

Why don’t you use x-position + your distance (float) as position?

I would prefer a solution that works without the x-position, because it is currently determined by the x-position of the bar end, and bar lengths in x-coordinates can vary by orders of magnitude. So to attain a somewhat constant clearance, I’d have to work with the x range and compute a fraction of it. --Possible but cumbersome. I wanted to either find a way without this step or at least find out in what forum or site to flag this behavior.

I think I do not understand the issue. I was referring to something like this.

import plotly.graph_objects as go
import numpy as np

YOUR_DISTANCE = 0.5

# dummy data
y_saving = [1.3586, 2.2623000000000002, 4.9821999999999997, 6.5096999999999996, 7.4812000000000003, 7.5133000000000001, 15.2148, 17.520499999999998]
y_net_worth = [93453.919999999998, 81666.570000000007, 69889.619999999995, 78381.529999999999, 141395.29999999999, 92969.020000000004, 66090.179999999993, 122379.3]
x = ['Japan', 'United Kingdom', 'Canada', 'Netherlands','United States', 'Belgium', 'Sweden', 'Switzerland']

# Creating two subplots
fig = go.Figure(
    go.Bar(
        x=y_saving,
        y=x,
        marker=dict(
            color='rgba(50, 171, 96, 0.6)',
            line=dict(
                color='rgba(50, 171, 96, 1.0)',
                width=1),
        ),
        name='Household savings, percentage of household disposable income',
        orientation='h',
    )
)


annotations = []

y_s = np.round(y_saving, decimals=2)
y_nw = np.rint(y_net_worth)

# Adding labels
for ydn, yd, xd in zip(y_nw, y_s, x):
    # labeling the bar net worth
    annotations.append(dict(
        xref='x1', 
        yref='y1',
        y=xd, 
        x=yd + YOUR_DISTANCE,
        text=str(yd) + '%',
        showarrow=False))

fig.update_layout(annotations=annotations)

fig.show()

Thanks for this reply! My issue rests in the fact that I don’t know beforehand the order of magnitude of the numbers that will be displayed. It can be 0…100 percent, or multiples of a billion. Therefore, expressing the clearance within the x coordinate system (YOUR_DISTANCE) is difficult and requires, I think, dealing with the overall x range and expressing the clearance as smth like “.0001 * <max. x>”. I get the impression that I will have to go this route.

*I would have liked the number positioning problem to be separate from the question of the data value range. By using the data-driven x range for a typographical matter seems to mix data and presentation, which feels more like drawing than plotting. [/opinion]