为特定的 x 轴刻度设置不同的字体颜色

Setting a different font color for specific x axis ticks(为特定的 x 轴刻度设置不同的字体颜色)
本文介绍了为特定的 x 轴刻度设置不同的字体颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个频率图,其中还绘制了 NA 值.我试图在 x 轴刻度中为 N/A 值着色不同.我知道如何在 matplotlib 中做到这一点,但似乎无法弄清楚如何使用 plotly 做到这一点.

我尝试使用值列表更新 tickcolors 和 tickfonts,但它只期望这两个属性都有一个值.请看下面的代码

# 不起作用 - plotly 期望 tickcolor 有一个值fig.update_xaxes(刻度角 = -60,tickcolor = ['黑色','黑色','黑色','黑色','红色'])# 在 matplotlib 中,以下代码可以正常工作# 它检查 xticklabels 的文本,如果等于 'N/A' 则改变颜色_ = [xl.set_color('red') for xl in plt.gca().get_xticklabels() if xl.get_text() == 'N/A/Missing']

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

几点说明:

  1. 我添加了一个空轨迹,否则我无法显示第二个轴.可能有一种方法可以在不这样做的情况下显示第二个轴,但我不知道如何.
  2. 必须在两者上设置相同的范围,否则第二个轴不会与第一个轴一起缩放,因为它们可以相互任意缩放/平移.
  3. 您必须手动指定刻度位置和值.从好的方面来说,这实际上对于这种特殊方法来说有点方便.
  4. side='bottom' 似乎不是必需的,至少对于这个情节来说,但它可能对其他人来说是明确的,所以......我把它放在这里.

<小时>

一方面,这种方法的好处是它在某种程度上独立于您使用的绘图类型.另一方面,最好不要通过坐标轴标签,而是通过信息的风格来传达信息的差异.例如,不同颜色的条形或类似颜色可能更能说明差异.

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:

  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 轴刻度设置不同的字体颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Leetcode 234: Palindrome LinkedList(Leetcode 234:回文链接列表)
How do I read an Excel file directly from Dropbox#39;s API using pandas.read_excel()?(如何使用PANDAS.READ_EXCEL()直接从Dropbox的API读取Excel文件?)
subprocess.Popen tries to write to nonexistent pipe(子进程。打开尝试写入不存在的管道)
I want to realize Popen-code from Windows to Linux:(我想实现从Windows到Linux的POpen-code:)
Reading stdout from a subprocess in real time(实时读取子进程中的标准输出)
How to call type safely on a random file in Python?(如何在Python中安全地调用随机文件上的类型?)