I'm creating a frequency plot with NA values also plotted. I'm trying to color the N/A values differently in x-axis tick. I know how to do this in matplotlib but can't seem to figure out how to do it using plotly.
I tried updating the tickcolors and tickfonts using a list of values but it just expects a single value for both of these attributes. Please see the code below
# Doesn't work - plotly expects a single value for tickcolor
fig.update_xaxes(
tickangle = -60,
tickcolor = ['black', 'black', 'black', 'black', 'red']
)
# In matplotlib the following code works fine
# It checks the text for xticklabels and changes color if it equals 'N/A'
_ = [xl.set_color('red') for xl in plt.gca().get_xticklabels() if xl.get_text() == 'N/A / Missing']
I want it to look like this - it's the output from my matplotlib code expected output
As I mentioned in my comment on the OP:
I'm fairly confident plotly does not give this ability directly. The only way I can think of to do it would be super convoluted: put two axes on your plot. One can be totally normal except the tick label for the red tick should be set to an empty string. The other axes would just have the one red tick label and all other labels set to empty string. And then position them so that they're on top of each other.
This definitely sucks, but it does work:
import plotly.graph_objs as go
data = [go.Scatter(
x=[1, 2, 3, 4],
y=[4, 5, 6, 7],
name="data"
), go.Scatter(xaxis='x2')]
layout = go.Layout(
xaxis=dict(
range=[0, 5],
title="xaxis title",
tickfont=dict(color="#1f77b4"),
tickmode='array',
tickvals=[1, 2, 3],
ticktext=['a', 'b', 'c'],
),
xaxis2=dict(
range=[0, 5],
tickfont=dict(color="#ff7f0e"),
tickmode='array',
tickvals=[4],
ticktext=['d'],
overlaying="x",
side="bottom",
)
)
fig = go.Figure(data=data, layout=layout)
fig.show()

A couple of notes:
side='bottom' doesn't seem to be necessary, at least for this plot, but it may for others and it's explicit anyways so...I kept it in here.On one hand, the nice thing about this method is that it is somewhat independent of what types of plots you're using. On the other hand, it may be better to convey the difference of information not by the axes labels, but by the style of the information. For e.g., a different colored bar or similar may be more indicative of the difference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With