更改ploty_express Parallel_category图的基本颜色图

发布于 2025-02-10 12:10:13 字数 514 浏览 2 评论 0原文

使用Ploty Express

import plotly.express as px

df = px.data.tips()
fig = px.parallel_categories(df)

fig.show()

”“在此处输入图像描述”

给出了带有蓝色基颜色的图。

我想在不使用图对象的情况下更改基本颜色。只是从蓝色到灰色的整体颜色。

在我的示例中,通过列分配颜色是不可能的。使用color_continouun_scale还针对不同的颜色更改。

感谢您的输入。

Creating a parallel_categories with ploty express

import plotly.express as px

df = px.data.tips()
fig = px.parallel_categories(df)

fig.show()

enter image description here

gives a diagram with blue base color.

I would like to change the base color without using graph object. Just the overall color from blue to e.g. gray.

Assigning a color via a column is in my example not possible. Using color_continuous_scale also aims at a different color change.

Thanks for your input.

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

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

发布评论

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

评论(2

成熟稳重的好男人 2025-02-17 12:10:13

Express

我发现可以通过plotly.express来实现这一点。创建一个颜色列表并指定连续的颜色尺度灰色。

import plotly.express as px
import numpy as np

color = np.zeros(len(df), dtype='uint8')

df = px.data.tips()
fig = px.parallel_categories(df, color=color, color_continuous_scale='gray')

fig.update_layout(coloraxis_showscale=False)
fig.show()

注意:
再次,输出图中存在差异。图表还添加了指定颜色的数据,该图将破坏Express的目的。

graph_objects

可以通过使用图形对象指定每个类别来创建相同的图。因此,我们正在创建一个数字列表,以赋予颜色0或1。然后,我们将颜色比例设置为0和1的灰色。此方法的灵感来自官方参考

import plotly.graph_objects as go
import plotly.express as px
import numpy as np

df = px.data.tips()

color = np.zeros(len(df), dtype='uint8')
colorscale = [[0, 'gray'],[1, 'gray']]

fig = go.Figure(go.Parcats(
    dimensions=[
        {'label': 'sex',
         'values':df['sex'].tolist()},
        {'label': 'smoker',
         'values':df['smoker'].tolist()},
        {'label': 'day',
         'values':df['day'].tolist()},
        {'label': 'time',
         'values':df['time'].tolist()},
        {'label': 'size',
         'values':df['size'].tolist()}],
    line={'colorscale':colorscale, 'cmin':0,'cmax':1,'color':color,'shape':'hspline'}
))

fig.show()

express

I have found that this can be achieved with plotly.express. Create a color list and specify a continuous color scale gray.

import plotly.express as px
import numpy as np

color = np.zeros(len(df), dtype='uint8')

df = px.data.tips()
fig = px.parallel_categories(df, color=color, color_continuous_scale='gray')

fig.update_layout(coloraxis_showscale=False)
fig.show()

enter image description here

Note:
Once again, there is a discrepancy in the output graph. The data specified for color is also added to the graph, which defeats the purpose of the express.

graph_objects

The same graph can be created by specifying each category using the graph object. So we are creating a list of numbers to give to the colors, either 0 or 1. Then we set the color scale to gray for both 0 and 1. This approach was inspired by the official reference.

import plotly.graph_objects as go
import plotly.express as px
import numpy as np

df = px.data.tips()

color = np.zeros(len(df), dtype='uint8')
colorscale = [[0, 'gray'],[1, 'gray']]

fig = go.Figure(go.Parcats(
    dimensions=[
        {'label': 'sex',
         'values':df['sex'].tolist()},
        {'label': 'smoker',
         'values':df['smoker'].tolist()},
        {'label': 'day',
         'values':df['day'].tolist()},
        {'label': 'time',
         'values':df['time'].tolist()},
        {'label': 'size',
         'values':df['size'].tolist()}],
    line={'colorscale':colorscale, 'cmin':0,'cmax':1,'color':color,'shape':'hspline'}
))

fig.show()

enter image description here

浮云落日 2025-02-17 12:10:13

这里的原始答案很有帮助,但错过了px.paralleal_categories() for dimensions 中有一个参数,您可以使用它来显示您想要的东西 - 那就是,不显示最终列,称为颜色。

以下代码与其他答案相同,除了dimensions = [...]包括(还将DF定义为订单)。

import plotly.express as px
import numpy as np

df = px.data.tips()
color = np.zeros(len(df), dtype="uint8")


fig = px.parallel_categories(
    df,
    dimensions=["sex", "smoker", "day", "time", "size"],
    color=color,
    color_continuous_scale="gray",
)

fig.update_layout(coloraxis_showscale=False)
fig.show()

Original answer here is helpful but missed that there is a parameter in px.parallel_categories() for dimensions, and you can use that to only show what you want - that is, to not show the final column called color.

The below code is the same as the other answer except has the dimensions=[...] included (and also the df was defined out of order).

import plotly.express as px
import numpy as np

df = px.data.tips()
color = np.zeros(len(df), dtype="uint8")


fig = px.parallel_categories(
    df,
    dimensions=["sex", "smoker", "day", "time", "size"],
    color=color,
    color_continuous_scale="gray",
)

fig.update_layout(coloraxis_showscale=False)
fig.show()

enter image description here

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