为特定的x轴刻度设置不同的字体颜色 [英] Setting a different font color for specific x axis ticks

查看:321
本文介绍了为特定的x轴刻度设置不同的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个频率图,其中还绘制了NA值.我正在尝试在X轴刻度线中为N/A值设置不同的颜色.我知道如何在matplotlib中执行此操作,但似乎无法弄清楚如何使用plotly.

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.

我尝试使用值列表来更新tickcolors和tickfonts,但它只是期望这两个属性具有单个值.请参见下面的代码

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']


我希望它看起来像这样-这是我的matplotlib代码预期的输出

I want it to look like this - it's the output from my matplotlib code expected output

推荐答案

正如我在对OP的评论中提到的:

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

一些注意事项:

  1. 我添加了一个空轨迹,否则我无法显示第二个轴.可能有一种不显示第二个轴的方法,但是我不知道如何做.
  2. 必须在两个轴上都设置相同的范围,否则第二个轴不能与第一个轴缩放,因为它们可以彼此任意缩放/转换.
  3. 您必须手动指定刻度位置和值.从好的方面来说,这对于这种特定方法实际上有点方便.
  4. side='bottom'似乎不是必需的,至少对于此情节而言,但这可能对其他情节来说是明确的,因此...我将其保留在此处.
  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.

这篇关于为特定的x轴刻度设置不同的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆