<bdo id='WIbD9'></bdo><ul id='WIbD9'></ul>

    <legend id='WIbD9'><style id='WIbD9'><dir id='WIbD9'><q id='WIbD9'></q></dir></style></legend>
  1. <i id='WIbD9'><tr id='WIbD9'><dt id='WIbD9'><q id='WIbD9'><span id='WIbD9'><b id='WIbD9'><form id='WIbD9'><ins id='WIbD9'></ins><ul id='WIbD9'></ul><sub id='WIbD9'></sub></form><legend id='WIbD9'></legend><bdo id='WIbD9'><pre id='WIbD9'><center id='WIbD9'></center></pre></bdo></b><th id='WIbD9'></th></span></q></dt></tr></i><div id='WIbD9'><tfoot id='WIbD9'></tfoot><dl id='WIbD9'><fieldset id='WIbD9'></fieldset></dl></div>
  2. <tfoot id='WIbD9'></tfoot>

      <small id='WIbD9'></small><noframes id='WIbD9'>

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

      时间:2023-09-28
            <legend id='EODha'><style id='EODha'><dir id='EODha'><q id='EODha'></q></dir></style></legend>

              <bdo id='EODha'></bdo><ul id='EODha'></ul>
                <tbody id='EODha'></tbody>

              <small id='EODha'></small><noframes id='EODha'>

              <tfoot id='EODha'></tfoot>
            • <i id='EODha'><tr id='EODha'><dt id='EODha'><q id='EODha'><span id='EODha'><b id='EODha'><form id='EODha'><ins id='EODha'></ins><ul id='EODha'></ul><sub id='EODha'></sub></form><legend id='EODha'></legend><bdo id='EODha'><pre id='EODha'><center id='EODha'></center></pre></bdo></b><th id='EODha'></th></span></q></dt></tr></i><div id='EODha'><tfoot id='EODha'></tfoot><dl id='EODha'><fieldset id='EODha'></fieldset></dl></div>

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

                上一篇:如何使用 plotly express 向折线图添加点或标记? 下一篇:在 plotly 中添加索引作为下拉菜单

                相关文章

                  <small id='ifLsm'></small><noframes id='ifLsm'>

                1. <i id='ifLsm'><tr id='ifLsm'><dt id='ifLsm'><q id='ifLsm'><span id='ifLsm'><b id='ifLsm'><form id='ifLsm'><ins id='ifLsm'></ins><ul id='ifLsm'></ul><sub id='ifLsm'></sub></form><legend id='ifLsm'></legend><bdo id='ifLsm'><pre id='ifLsm'><center id='ifLsm'></center></pre></bdo></b><th id='ifLsm'></th></span></q></dt></tr></i><div id='ifLsm'><tfoot id='ifLsm'></tfoot><dl id='ifLsm'><fieldset id='ifLsm'></fieldset></dl></div>
                2. <legend id='ifLsm'><style id='ifLsm'><dir id='ifLsm'><q id='ifLsm'></q></dir></style></legend>
                    <bdo id='ifLsm'></bdo><ul id='ifLsm'></ul>
                  <tfoot id='ifLsm'></tfoot>