i200yrs
(Rhett)
October 4, 2023, 2:34am
1
Hello All…need some advice, i got this sample data below:
Test# ch1 ch2
1 1 8
2 1 8
3 1 8
4 1 1
5 5 1
6 5 1
7 5 1
8 1 1
9 5 1
1 = signifies pass test
5 = fail contact
8 = fail fucntional
I make a chart as below result, please anyone can help me please. Thanks in advance
Expected result as below:
bryevdv
(Bryan Van de Ven)
October 4, 2023, 4:20am
2
Here’s how you could do it with Bokeh :
import pandas as pd
from bokeh.models import Legend
from bokeh.plotting import figure, show
from bokeh.transform import factor_cmap
df = pd.DataFrame({
"test": list(range(1, 10)),
"ch1": [1,1,1,1,5,5,5,1,5],
"ch2": [8,8,8,1,1,1,1,1,1],
})
COLORS = ("green", "red", "yellow")
VALUES = ("1", "5", "8")
# long format with string factors ergonomic for Bokeh
df = df.melt(["test"], ["ch1", "ch2"], "channel")
df.value = df.value.astype(str)
p = figure(width=400, x_range=("ch1", "ch2"), toolbar_location=None)
# Add legend first since we want to manually position
p.add_layout(Legend(location="center"), "right")
p.rect(x="channel", y="test", width=1, height=1,
source=df, legend_group="value",
color=factor_cmap("value", COLORS, VALUES))
p.y_range.range_padding = 0
p.yaxis.ticker = list(range(1, 10))
show(p)
1 Like
i200yrs
(Rhett)
October 4, 2023, 5:41am
3
Bryan Van de Ven:
import pandas as pd
from bokeh.models import Legend
from bokeh.plotting import figure, show
from bokeh.transform import factor_cmap
df = pd.DataFrame({
"test": list(range(1, 10)),
"ch1": [1,1,1,1,5,5,5,1,5],
"ch2": [8,8,8,1,1,1,1,1,1],
})
COLORS = ("green", "red", "yellow")
VALUES = ("1", "5", "8")
# long format with string factors ergonomic for Bokeh
df = df.melt(["test"], ["ch1", "ch2"], "channel")
df.value = df.value.astype(str)
p = figure(width=400, x_range=("ch1", "ch2"), toolbar_location=None)
# Add legend first since we want to manually position
p.add_layout(Legend(location="center"), "right")
p.rect(x="channel", y="test", width=1, height=1,
source=df, legend_group="value",
color=factor_cmap("value", COLORS, VALUES))
p.y_range.range_padding = 0
p.yaxis.ticker = list(range(1, 10))
show(p)
Thanks a lot…how to modify the code using p.show() ? and also save the image?
Or can we store this plot to variable/function that can use later? thanks
bryevdv
(Bryan Van de Ven)
October 4, 2023, 3:01pm
4
There is no show
method in Bokeh, only the function above. Perhaps you are thinking of Matplotlib ? That is a different library and has a very different API.
and also save the image?
Bokeh is mostly intended for presentation in browsers but can export PNG and SVG with some optional dependencies installed: PNG and SVG export — Bokeh 3.2.2 Documentation
1 Like
i200yrs
(Rhett)
October 5, 2023, 1:36am
5
Thanks…can we possibly make same chart using Matplotlib? thanks
bryevdv
(Bryan Van de Ven)
October 5, 2023, 4:09am
6
can we possibly make same chart using Matplotlib?
Almost certainly, but someone else will have to chime in, as I am not a frequent user of Matplotlib,
1 Like