如何在px.treemap中的textemplate中显示用于颜色的值

发布于 2025-01-31 05:08:13 字数 1078 浏览 2 评论 0原文

我正在使用Plotly Treemap进行第一次工作。我想显示用于块中颜色的值。

使用 this 答案,我能够创建custy> custydata,但是treemap渲染值,但是,值在某些情况下显示不同记录的“增长”值的顺序与数据框架的顺序不同,它显示了“增长”值。它也不是偏离的。

示例数据:

Type,Stock,Holding,Growth
Equities,Sasol,500,118.06
ETFs,SATRIX Top 40,500,12.99

代码:

shares_data = pd.read_csv("stocks.csv", index_col=False)

fig = px.treemap(shares_data,
                 path=['Type', 'Stock'],
                 values='Holding',
                 color='Growth',
                 color_continuous_scale=[(0, "red"), (0.05, "yellow"), (1, "green")],
                 )

fig.data[0].customdata = shares_data.Growth

fig.data[0].texttemplate = "<b>%{label}</b><br>Holding: R%{value}<br>Growth: %{customdata:.2f}%<br>"

fig.show()

渲染:

“渲染”

I'm doing my first work with a Plotly Treemap. I want to show the values used for the colors in the blocks.

Using this answer, I was able to create customdata, and the treemap renders however, the values are not in the same order as the dataframe and it shows the 'Growth' value for a different record in some cases. It is also not off-by-one.

Sample data:

Type,Stock,Holding,Growth
Equities,Sasol,500,118.06
ETFs,SATRIX Top 40,500,12.99

Code:

shares_data = pd.read_csv("stocks.csv", index_col=False)

fig = px.treemap(shares_data,
                 path=['Type', 'Stock'],
                 values='Holding',
                 color='Growth',
                 color_continuous_scale=[(0, "red"), (0.05, "yellow"), (1, "green")],
                 )

fig.data[0].customdata = shares_data.Growth

fig.data[0].texttemplate = "<b>%{label}</b><br>Holding: R%{value}<br>Growth: %{customdata:.2f}%<br>"

fig.show()

Rendered:

Rendered

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

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

发布评论

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

评论(1

凉薄对峙 2025-02-07 05:08:13

阅读 this ,我能够打印无花果的数据,其中包括:

 'marker': {'coloraxis': 'coloraxis',
           'colors': array([ 53.02, 25.17, -23.29, 10.21, 57.59,...

然后我可以使用以下方式访问值:

fig.data[0].customdata = fig.data[0].marker.colors

现在可以使用。

[因为我真的很挣扎,所以我决定发布答案,而不是删除问题,以防将来有人发现有用。]

Having read this, I was able to print the fig's data, which included:

 'marker': {'coloraxis': 'coloraxis',
           'colors': array([ 53.02, 25.17, -23.29, 10.21, 57.59,...

So I could then access the values with:

fig.data[0].customdata = fig.data[0].marker.colors

And now it works.

[Because I really struggled with this, I decided to post the answer instead of deleting the question in case someone else in the future finds ity useful.]

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