绘图对象在两种形式内改变颜色

发布于 2025-01-13 22:00:23 字数 2758 浏览 2 评论 0原文

我正在研究plotly.graph_objects。这是我的结果:

在此处输入图像描述

我想更改绿线和黑线之间的第二个块(15 和 35)内的颜色(例如红色)。

这是我的代码:

from cgitb import text
import plotly.graph_objects as go
import numpy as np

labels = [5, 17, 39]


widths = np.array([10,20,10])

data = {
    "x1": [75,75,30],
    "x2": [75,0,45],
    "x3": [0,75,75],
}

x = [0, 4 , 12]
fig = go.Figure()

color = {
    "x1": '#E5EBF7',
    "x2":  '#D9CD9F',
    "x3":  '#F7F4E9',
}
for key in data:
    fig.add_trace(
        go.Bar(
        name=key,
        y=data[key],
        x= labels,

        width=widths,
        offset=0,
        customdata=np.transpose([labels, widths*data[key]]),
        texttemplate="%{y} x %{width} =<br>%{customdata[1]}",
        
        marker_color = color[key],
        textfont_color="white",
        textposition="inside",
        textangle=0,
        
        # hovertemplate="<br>".join([
        #     "label: %{customdata[0]}",
        #     "width: %{width}",
        #     "height: %{y}",
        #     "area: %{customdata[1]}",
        # ])
    ))




fig.update_layout({
    'plot_bgcolor': 'rgba(0, 0, 0, 0)',
    })


#les lignes
fig.add_shape( # add a horizontal "target" line
    type="line", line_color="black", line_width=3, opacity=1,
    x0=0, x1=55, y0=0, y1=0
)
fig.add_shape( # add a vertigal "target" line
    type="line", line_color="black", line_width=3, opacity=1,
    x0=4.5, x1=4.5, y0=0, y1=150
)


fig.add_shape( # add a lignes premier block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=5, x1=15, y0=75, y1=75
)

fig.add_shape( # add la ligne horizontale deuxieme block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=17, x1=37, y0=75, y1=75
)


fig.add_shape( # add la ligne horizontale deuxieme block line degressive
    type="line", line_color="green", line_width=3, opacity=1, line_dash="dot",
    x0=17, x1=37, y0=100, y1=77.5
)

fig.add_shape( # add la ligne horizontale troisieme block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=39, x1=49, y0=75, y1=75
)



fig.update_layout(barmode="stack",uniformtext=dict(mode="hide", minsize=10),
)



#ICI FAIRE EN SORTE S IL Y A 2 OU 3 BLOCS (OU MANIERE DYNAMIQUE MAIS JE N Y ARRIVE PAS)

#le premier parametre de range x, permet de mettre ou non un blanc entre le 0 et le premier bloc
fig.update_xaxes(range=[4,55])
fig.update_yaxes(range=[0,130])
fig.show()

我检查了文档和教程示例,但没有找到任何内容。 我猜它是这样的:“当 x > 15 且 x < 37 且 y > 75 且 y < green.line.y 时”无花果是绿色的

如果有人有任何解决方案或建议。 谢谢!

I'm working on plotly.graph_objects. There is my result:

enter image description here

and i would like to change the color (example red) inside my 2nd block (15 at 35) between the green line and the black line.

there is my code:

from cgitb import text
import plotly.graph_objects as go
import numpy as np

labels = [5, 17, 39]


widths = np.array([10,20,10])

data = {
    "x1": [75,75,30],
    "x2": [75,0,45],
    "x3": [0,75,75],
}

x = [0, 4 , 12]
fig = go.Figure()

color = {
    "x1": '#E5EBF7',
    "x2":  '#D9CD9F',
    "x3":  '#F7F4E9',
}
for key in data:
    fig.add_trace(
        go.Bar(
        name=key,
        y=data[key],
        x= labels,

        width=widths,
        offset=0,
        customdata=np.transpose([labels, widths*data[key]]),
        texttemplate="%{y} x %{width} =<br>%{customdata[1]}",
        
        marker_color = color[key],
        textfont_color="white",
        textposition="inside",
        textangle=0,
        
        # hovertemplate="<br>".join([
        #     "label: %{customdata[0]}",
        #     "width: %{width}",
        #     "height: %{y}",
        #     "area: %{customdata[1]}",
        # ])
    ))




fig.update_layout({
    'plot_bgcolor': 'rgba(0, 0, 0, 0)',
    })


#les lignes
fig.add_shape( # add a horizontal "target" line
    type="line", line_color="black", line_width=3, opacity=1,
    x0=0, x1=55, y0=0, y1=0
)
fig.add_shape( # add a vertigal "target" line
    type="line", line_color="black", line_width=3, opacity=1,
    x0=4.5, x1=4.5, y0=0, y1=150
)


fig.add_shape( # add a lignes premier block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=5, x1=15, y0=75, y1=75
)

fig.add_shape( # add la ligne horizontale deuxieme block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=17, x1=37, y0=75, y1=75
)


fig.add_shape( # add la ligne horizontale deuxieme block line degressive
    type="line", line_color="green", line_width=3, opacity=1, line_dash="dot",
    x0=17, x1=37, y0=100, y1=77.5
)

fig.add_shape( # add la ligne horizontale troisieme block line
    type="line", line_color="black", line_width=3, opacity=1, line_dash="dot",
    x0=39, x1=49, y0=75, y1=75
)



fig.update_layout(barmode="stack",uniformtext=dict(mode="hide", minsize=10),
)



#ICI FAIRE EN SORTE S IL Y A 2 OU 3 BLOCS (OU MANIERE DYNAMIQUE MAIS JE N Y ARRIVE PAS)

#le premier parametre de range x, permet de mettre ou non un blanc entre le 0 et le premier bloc
fig.update_xaxes(range=[4,55])
fig.update_yaxes(range=[0,130])
fig.show()

I checked on the documentation and on the tutorial example but i don't find anything.
I guess it was something like: "while x > 15 and x < 37 and y > 75 and y < green.line.y " fig is green

If someone has any solution or advice.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

待"谢繁草 2025-01-20 22:00:23

最简单的方法是在散点图上使用线条模式添加三角形,指定从 x 轴和 y 轴的起点到起点的四个点。指定任意颜色作为填充。

fig.add_trace(go.Scatter(x=[17,37,17,17],
                         y=[77.5,77,100,77],
                         fill='toself',
                         fillcolor='#D9CD9F',
                         line=dict(width=0),
                         showlegend=False,
                         mode='lines'))

输入图片此处描述

The easiest way to do this is to use line mode on a scatter plot to add triangles, specifying four points from the start of the x-axis and y-axis to the starting point. Specify any color as the fill.

fig.add_trace(go.Scatter(x=[17,37,17,17],
                         y=[77.5,77,100,77],
                         fill='toself',
                         fillcolor='#D9CD9F',
                         line=dict(width=0),
                         showlegend=False,
                         mode='lines'))

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文