Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

python - Setting a different font color for specific x axis ticks

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

Multiple tick colors

A couple of notes:

  1. I added an empty trace, otherwise I couldn't get the second axis to show up. There might be a way to show the second axis without doing that, but I couldn't figure out how.
  2. The range has to be set the same on both, otherwise the second axis is not scaled with the first, since they can be scaled/translated arbitrarily from each other.
  3. You have to manually specify the tick locations and values. On the plus side, this is actually somewhat convenient for this particular method.
  4. The 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...