为特定的 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 代码的输出
几点说明:
- 我添加了一个空轨迹,否则我无法显示第二个轴.可能有一种方法可以在不这样做的情况下显示第二个轴,但我不知道如何.
- 必须在两者上设置相同的范围,否则第二个轴不会与第一个轴一起缩放,因为它们可以相互任意缩放/平移.
- 您必须手动指定刻度位置和值.从好的方面来说,这实际上对于这种特殊方法来说有点方便.
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:
- 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.
- 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.
- You have to manually specify the tick locations and values. On the plus side, this is actually somewhat convenient for this particular method.
- 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.
相关文章