如何避免在绘图中复制图例标签或传递自定义图例标签

2024-05-29 12:00:12 发布

您现在位置:Python中文网/ 问答频道 /正文

如何避免子批次中出现重复的图例标签?在matplotlib中,一种方法是将自定义图例标签传递给图例对象。我在plotly中找不到任何与此类似的文档。有什么想法吗?

traces = []

colors = {'Iris-setosa': 'rgb(31, 119, 180)', 
          'Iris-versicolor': 'rgb(255, 127, 14)', 
          'Iris-virginica': 'rgb(44, 160, 44)'}

for col in range(4):
    for key in colors:
        traces.append(Histogram(x=X[y==key, col], 
                        opacity=0.75,
                        xaxis='x%s' %(col+1),
                        marker=Marker(color=colors[key]),
                        name=key
                        )
                     )

data = Data(traces)

layout = Layout(barmode='overlay',
                xaxis=XAxis(domain=[0, 0.25], title='sepal length (cm)'),
                xaxis2=XAxis(domain=[0.3, 0.5], title='sepal width (cm)'),
                xaxis3=XAxis(domain=[0.55, 0.75], title='petal length (cm)'),
                xaxis4=XAxis(domain=[0.8, 1], title='petal width (cm)'),
                yaxis=YAxis(title='count'),
                title='Distribution of the different Iris flower features')

fig = Figure(data=data, layout=layout)

py.iplot(fig)

enter image description here


Tags: keyirisfordatatitledomaincmcol

热门问题