python - 将颜色图添加到图表中

标签 python matplotlib plotly

import plotly
import plotly.graph_objs as go
import matplotlib.pyplot as plt
import numpy as np

plotly.offline.init_notebook_mode(connected=True)
colorscale='Earth'
#print("     :  Min  :   Q1   : Median :   Q3   :    Max  :  Mean  : Good Mean : Good STD : Outliers :")
data = [
    {
        'x': ['Min','Min','Min','Min','Min'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.89,0.892,0.886,0.901,0.869],
            'size': [30, 30, 30, 30, 30],
            'showscale': False,
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.865,
            'cmax':0.901,
        }
    },
    {
        'x': ['Q1','Q1','Q1','Q1','Q1'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.912,0.908,0.892,0.915,0.889],
            'size': [30, 30, 30, 30, 30],
            'showscale': False, 
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.885,
            'cmax':0.915,
        }
    },
    {
        'x': ['Median','Median','Median','Median','Median'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.919,0.912,0.914,0.917,0.9],
            'size': [30, 30, 30, 30, 30],
            'showscale': False,
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.89,
            'cmax':0.919,
        }
    },
    {
        'x': ['Q3','Q3','Q3','Q3','Q3'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.929,0.919,0.925,0.922,0.909],
            'size': [30, 30, 30, 30, 30],
            'showscale': False,
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.90,
            'cmax':0.929,
        }
    },
    {
        'x': ['Max','Max','Max','Max','Max'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.95,0.932,0.933,0.935,0.935],
            'size': [30, 30, 30, 30, 30],
            'showscale': False, 
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.93,
            'cmax':0.95,
        }
    },
    {
        'x': ['Mean','Mean','Mean','Mean','Mean'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers',
        'marker': {
            'color': [0.921,0.912,0.91,0.918,0.9],
            'size': [30, 30, 30, 30, 30],
            'showscale': False, 
            'colorscale':colorscale,
            'reversescale':True,
            'cmin':0.898,
            'cmax':0.921,
        }
    }
]
layout = go.Layout(title='Parameters of evaluation', xaxis=dict(range=[-0.5, 10]), yaxis=dict(range=[-0.5, 5.5]))
fig = go.Figure(data=data, layout=layout)                           

#plotly.offline.iplot(data, filename='scatter-colorscale')
plotly.offline.iplot(fig)

大家好,我正在做一个可视化,今天刚开始用plotly,它太酷了。无论如何,我希望在图的侧面添加颜色图,无论我选择什么颜色方案,对我来说可能不可能找到全局颜色图,因为我的颜色渐变起点和终点不同。但是我怎样才能至少在侧面添加一个颜色图来显示最高颜色和最低颜色。有简单的方法吗?

I want to add colormap like this, at right hand side

The output image i have right now looks like this, apart from the trace and title etc which I have to remove later, somehow. I wanted to do this, in a proper way

最佳答案

{
        'x': ['Mean','Mean','Mean','Mean','Mean'],
        'y': ['config1','config2','config3','config4','config5'],
        'mode': 'markers,text',
        'marker': {
            'colorbar':dict(nticks=3,tickmode='array',tickvals=[2,4.5,7],showticklabels=True,ticktext=['lowest','middle','highest']),
            'size': [30, 30, 30, 30, 30],
            'showscale': True, 
            'colorscale':colorscale,
            'reversescale':True,
            'opacity':0
        }

    }

我添加了这样的 hack 之一,以暂时解决我的问题。其他人可以建议我更好的方法。我添加了一条不透明度为零的额外迹线,并为其添加了自定义颜色条。它使我能够添加一个不会与其他数据混淆的颜色条。我也可以在任何跟踪中添加此颜色条。

关于python - 将颜色图添加到图表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54029544/

相关文章:

python - Pygame 运行几秒钟后变得无响应

python - 在 plt.plot() 命令之外定义绘图标签 - Matplotlib

python - Plotly:将等高线图更改为 3 维曲面

javascript - 在 JavaScript 中检索 R 对象属性

python - 如何巧妙地不忽略我的轴设置?

python - numpy.einsum 的输出形状

python - 如何使用 python 将 XLSB 文件转换为 csv?

python - 如何仅在 Python 中升级虚拟环境中的包?

python - 将 Matplotlib 日期数组转换为 NaN 的 Datetime 对象

python - 带有 python 和 matplotlib 中线条的地理数据图/ map