I would like to make a plot with matplotlib in python with rectangle lines likes this one:
Sadly I have no idea how to call this kind of plot (I photoshoped this one together, it’s kind of like a bar plot but rotated by 90 degrees), therefore my google search is a lost case. My dataset is like this:
y x
1.0 1.0
0.6 2.3
0.3 3.3
0.1 1.4
-0.4 2.1
-0.6 3.8
-0.8 0.9
-1.0
I already put x
and y
into two lists of variables. Basically the x
value should be drawn in between the current and the next y
value (compare the picture and the values in the example).
3
Answers
You should try
matplolib.path
, Try the following codeYou will get a basic understanding of how path works and then modify your vertices to plot your own graph. For more info, please refer to the following link:
https://matplotlib.org/api/path_api.html#module-matplotlib.path
If you want to use
plt.plot
, you could also add extra vertices. So instead of:Do
giving you:
Of course, you might notice that we’re just repeating the values of
x
twice (with the last value removed) andy
twice (with the first value removed), so an easy way to convertx
tonew_x
andy
tonew_y
with pure Python is:It would be even cleaner using
itertools
ornp.repeat
.You can use the
"steps-"
linestyle for plotting, ("steps-pre"
,"steps-post"
,"steps-mid"
). Those determine whether the coordinates are meant to denote the beginning, the middle and the end of the line.Some complete example: