1
$\begingroup$

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 :

enter image description here

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

$\endgroup$
2
  • $\begingroup$ Update your post with what you have tried and why it is not working. $\endgroup$
    – Edmund
    May 31 '19 at 23:38
  • $\begingroup$ 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? $\endgroup$
    – Lan Phuong
    Jun 2 '19 at 10:07
0
$\begingroup$

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.Thread(wl.Rule(wl.Range(3),[wl.Green,wl.Orange,wl.Blue])),
            wl.Rule(wl.Blank(),wl.White)]
        ),
        FrameTicks = [
            [wl.Thread([range(1,4), ['A','B','C']]), wlexpr('None')],
            [
                wl.Thread([ 
                    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
    )
)

Mathematica graphics

Terminate session

wolfSession.terminate()

Hope this helps.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Lan Phuong
    Jun 5 '19 at 7:57
  • $\begingroup$ @LanPhuong You need to download the free engine that goes with the wolframclient package; you can fine it here. $\endgroup$
    – Edmund
    Jun 5 '19 at 9:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.