Plotly barchart (graph object GO) 不同的标记模式形状取决于特定的列值

Plotly barchart (graph object GO) different marker pattern shape depending on the specific column value

[由@Rob Raymond 第二个答案解决]

我一直在努力定义模式形状源(特定列值)。

我想根据列名定义模式形状(“//”、“.”等)'size'。

最小可重现示例:

import plotly.graph_objects as go
import pandas as pd
animals=['giraffes', 'orangutans', 'monkeys', 'rats']
size=['big', 'medium', 'medium', 'small']
number=[20, 14, 23, 5]
columns = ['animal','size','number','test']

df = pd.DataFrame({'animals':animals, 'size':size,'number':number})
fig = go.Figure([go.Bar( x=df['animals'].values.tolist(), y=df['number'].values.tolist())])
fig.update_traces(marker_pattern_shape=['.', '\','+','/'])
fig.show()
animals size number
1 giraffes big 20
2 orangutans medium 14
3 monkeys medium 23
4 rats small 5

我一直在尝试以下几行:
fig.update_traces(marker_pattern_shape=['.', '\','+','/'], marker_pattern = {'shape':['+', '\']})
go.Bar(..., marker = {'pattern':'.'}, ...)


PS。我被迫使用 plotly GO 而不是 plotly express。说来话长。

@Rob Raymond 回答后更新。

它适用于第一个解决方案,但不适用于我创建的弗兰肯斯坦。下面是更复杂的最小可重现示例:

import plotly.graph_objects as go
import pandas as pd
animals=['giraffes', 'orangutans', 'monkeys', 'rats', "mice"]
size=['big', 'medium', 'medium', 'small', "small"]
number=[20, 14, 23, 5, 4]
animalId=[1,2,3,4,5]
colorPlotly = ['lightslategrey','red','black', 'saddlebrown','grey']

df = pd.DataFrame({'animals':animals, 'size':size,'number':number,'animalId':animalId, 'colorPlotly':colorPlotly})
fig = go.Figure()
for id in animalId:
    fig.add_trace(go.Bar(
        x=df[df['animalId']==id]['animals'],
            y=df[df['animalId']==id]['number'],
                name = id,
                    marker_color=df[df['animalId']==id]['colorPlotly']))

fig.update_traces(marker_pattern_shape=df["size"].map({'big':".", 'medium':"\", 'medium':"+", 'small':"/"}))
fig.show()

animals size number colorPlotly animalId
1 giraffes big 20 lightslategrey 1
2 orangutans medium 14 red 2
3 monkeys medium 23 black 3
4 rats small 4 saddlebrown 4
5 mices small 5 grey 5

import plotly.graph_objects as go
import pandas as pd
animals=['giraffes', 'orangutans', 'monkeys', 'rats', "mice"]
size=['big', 'medium', 'medium', 'small', "small"]
number=[20, 14, 23, 5, 4]

df = pd.DataFrame({'animals':animals, 'size':size,'number':number})
fig = go.Figure([go.Bar( x=df['animals'], y=df['number'])])
fig.update_traces(marker_pattern_shape=df["size"].map({'big':".", 'medium':"\", 'medium':"+", 'small':"/"}))
fig.show()


使用多个跟踪

  • 在动物名称和有效的模式形状
  • 之间建立一个映射(dict
  • 构造图形时使用它
animal_map = {k:v for k, v in zip(animals, np.tile(['/', '\', 'x', '-', '|', '+', '.'], 3)[:len(animals)])}
fig = go.Figure()
for id in animalId:
    fig.add_trace(
        go.Bar(
            x=df[df["animalId"] == id]["animals"],
            y=df[df["animalId"] == id]["number"],
            name=id,
            marker_color=df[df["animalId"] == id]["colorPlotly"],
            marker_pattern_shape=df.loc[df["animalId"] == id, "animals"].map(animal_map)
        )
    )
fig.show()