import plotly.graph_objs as go
# Define data to be plotted
data = [go.Bar(
y=['long text', 'B', 'short', 'well'],
x=[10, 20, 30, 40],
orientation='h'
)]
# Create a Figure object
fig = go.Figure(data=data)
# Show the figure
fig.show()
I want to center the y ticks to be below each other in the middle how can I achieve this?
I did not figure out how to format strings, which is your case since you use categorical data on the y-axis. I am not sure, if it’s even possible with strings, but I did not invest too much time, honestly.
For numeric data this is an example, even if the result is not perfectly aligned:
import plotly.graph_objs as go
# Define data to be plotted
data = [go.Bar(
# y=['long text', 'B', 'short', 'well'],
y=[1000, 200, 90, 9],
x=[10, 20, 30, 40],
orientation='h'
)]
# Create a Figure object
fig = go.Figure(data=data)
# Show the figure
fig.update_layout(yaxis={'tickformat': '.^20'})
# ^^ this centers the number, and fills right & left with '.' up to a total width of 20 characters
Seems like it’s not automatique in plotly. So you juste have to play with your ylist so that every element of the list have same length. You can create a simple function for that.
import plotly.graph_objs as go
# Define data to be plotted
data = [go.Bar(
y=['long text', ' B ', ' short ', ' well '],
x=[10, 20, 30, 40],
orientation='h'
)]
fig = go.Figure(data=data)
fig.update_yaxes(ticks = "outside", tickcolor='black', ticklen=10, tickwidth = 0.5)
fig
I did not share before because it’s an ugly code . It just something like that
@AIMPED@AbdelAnlah Hi guys sorry for answering in such a delay!
First thanks for the answers!
I did notice it doesn’t always work. because letters have different sizes so depending on the letters the size of the ytick g element changes
Using what you guys sent me I have one element that is 15 pixels bigger than the other elements. which causes them not to be aligned.
So it works in some cases but sadly not in my case.
If you have any other ideas please do let me know!
It would be nice if the ticktext argument accepted a html Div.
I noticed, that some html tags work on it, such as <b> and <a>.
text = ['long text', 'B', 'short', 'well']
test = [f'<a href="www.google.com">{item}</a>' for item in text]
test = [f'<b>{item}</b>' for item in text]
# ^^ works
test = [f'<center>{item}</center>' for item in text]
# ^^ does not work
There is a simpler way;
Add annotation, with xanchor=“center”, and set showticklabels=False.
Set yref to y, y domain or paper.
And you wil get perfectly centered ticklabels