【问题标题】:Color Dictionary is not respected by Plotly in PythonPlotly 在 Python 中不尊重颜色字典
【发布时间】:2021-12-05 22:59:00
【问题描述】:

我正在使用 Streamlit 创建一个仪表板,但我发现了一些奇怪的东西,我的绘图正在可视化一个聚类算法,其中包含一个用于聚类数量的交互式数字输入。所以我希望如果我有 2 个输入,我将始终获得与我的标记颜色相同的值。

但是,每次我与网站交互时,颜色都会发生变化,我必须为我的集群获取这些颜色。

def plot_model(cluster_model):
    cluster_color_dict = {
        1:'yellow',
        2:'CornflowerBlue',
        3:'orange',
        4:'green',
        5:'black',
        6:'fuchsia',
        7:'red',
        8:'aqua',
        9:'deepskyblue',
        10:'greenyellow'
    }

    fig = px.scatter_3d(cluster_model,
                    x='NPHI',
                    y='RHOZ',
                    z='GR',
                    color='Clusters',
                    color_continuous_scale=list(cluster_color_dict.values()))

    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    fig.update_traces(marker_size=3)
    return fig

无论如何,我认为也许一种方法是简单地生成一个数字数组来修剪字典的长度,使其始终具有用户请求的颜色?

【问题讨论】:

  • 我不明白你的问题。使用示例数据更好地创建最小的工作代码,显示当前代码的输出和您期望的输出。

标签: python plotly streamlit


【解决方案1】:

我遇到的问题是字典包含 10 个元素,但是传递给函数来创建图形的元素数量较少,所以如果我想有一致的颜色,我只需要传递元素的数量然后过滤字典,这样我的图表中总是会得到相同的配色方案:

def plot_model(cluster_model,k):
    template = {
        1:'yellow',
        2:'CornflowerBlue',
        3:'orange',
        4:'green',
        5:'black',
        6:'fuchsia',
        7:'red',
        8:'aqua',
        9:'deepskyblue',
        10:'greenyellow'
    }
    i,j = 1,k
    cluster_color_dict = {}
    for k,v in template.items():
        if int(k) >= i and int(k) <= j:
            cluster_color_dict[k] = v




    fig = px.scatter_3d(cluster_model,
                    x='NPHI',
                    y='RHOZ',
                    z='GR',
                    color='Clusters',
                    color_continuous_scale=list(cluster_color_dict.values()))

    fig.update_layout(margin=dict(l=0, r=0, b=0, t=0))
    fig.update_traces(marker_size=3)
    return

【讨论】:

  • 如果这对您有用,请考虑接受它:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-07
  • 1970-01-01
相关资源
最近更新 更多