How to chart fail occurence in number of test using python

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:


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))


1 Like

Thanks a lot…how to modify the code using ? and also save the image?
Or can we store this plot to variable/function that can use later? thanks

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

Thanks…can we possibly make same chart using Matplotlib? thanks

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