Setting a different font color for specific x axis ticks

╄→гoц情女王★ 提交于 2020-01-16 04:20:11

问题


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


回答1:


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:

  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.



来源:https://stackoverflow.com/questions/57549944/setting-a-different-font-color-for-specific-x-axis-ticks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!