How to create a stacked bar chart with gaps between values of each variables in Python

I have a problem with creating a stacked bar chart in Python. I have data with 3 variables as below:

A=[3,5,7] B=[4,5,7] C=[2,3,4,5,6,7]

I would like to create a bar chart with gaps of values of each variable as below bar chart with gaps between values of each variable :

Could anyone know how to create it in Python? Thank you very much.

• Update your post with what you have tried and why it is not working. May 31 '19 at 23:38
• I don't know what kind of barchart here so don't know how to handle it? could you give any idea about what kind of chart? Jun 2 '19 at 10:07

You may apply Wolfram Language to your project. There is a free Wolfram Engine for developers and with the Wolfram Client Library for Python you can use these functions in Python.

from wolframclient.evaluation import WolframLanguageSession
from wolframclient.language import wl, wlexpr


Start the session.

wolfSession = WolframLanguageSession();
A=[3,5,7];
B=[4,5,7];
C=[2,3,4,5,6,7];


Either the ArrayPlot or MatrixPlot functions can be applied. These functions take 2D matrices so first need to convert the lists.

With SparseArray a matrix can be specified without explicitly setting values for all positions. Empty positions are set to '' below.

pos=wolfSession.evaluate(
wl.Flatten(
wl.MapIndexed(
wl.Function(wl.Rule([wl.First(wl.Slot(2)), wl.Slot(1)], wl.First(wl.Slot(2)))),
[A,B,C],
[2]
),
1
)
);
sparse = wolfSession.evaluate(wl.SparseArray(pos, wl.Automatic, ''));

print(wolfSession.evaluate(wl.Normal(sparse)))

[['', '', 1, '', 1, '', 1],
['', '', '', 2, 2, '', 2],
['', 3, 3, 3, 3, 3, 3]]


Using custom ColorRules and FrameTicks to get a basic representation of your plot. You can use these and many other options to get you precise look you

stacked=wolfSession.evaluate(
wl.ArrayPlot(
sparse,
AspectRatio = 1/4,
ColorRules = wl.Flatten(
wl.Rule(wl.Blank(),wl.White)]
),
FrameTicks = [
[
wl.Range(7),
list(map(lambda r: str(r)+'k', range(1,8)))
]),
wlexpr('None')
]
]
)
);


Then Export in one of the supported Raster Image Formats or Vector Graphics Formats.

wolfSession.evaluate(
wl.Export(
'<path with image filename>',
stacked
)
)


Terminate session

wolfSession.terminate()


Hope this helps.

• Thank you for your answer but I can't run the code. When I run wolfSession = WolframLanguageSession(); The error is ValueError: Invalid kernel value. Expecting a filepath as a string. I try to google the error but can't find the reason. Jun 5 '19 at 7:57
• @LanPhuong You need to download the free engine that goes with the wolframclient package; you can fine it here. Jun 5 '19 at 9:55