Home > Enterprise >  Plot two bar plots on top of each other and colour change between the two
Plot two bar plots on top of each other and colour change between the two

Time:06-03

I currently have two sets of data. One is an old set of data, and the other is a new set. This data is time series data, so the x axis is a month, and the y axis is the integer value. I would like to compare the two using a bar plot, showing the changes on that bar plot from the old data to the new data in an overlayed bar plot.

If the value in the new data is lower than the value in the old data for the corresponding date, the bar for that particular date would have the change (delta) in red, showing the decrease.

If the value in the new data is higher than the value in the old data for the corresponding date, the bar for that particular date would have the change (delta) in green, showing the increase, adding on to the bar for the old value.

Some synthetic data is below - this is a combination of the new and old sets of data into an aggregated form to make it easier to plot.

DATE OLD VALUE NEW VALUE DELTA
08/22 10000 9500 -500
09/22 5000 5500 500
10/22 8000 9000 1000
11/22 20000 18000 -2000

I've tried overlaying two bar plots on top of each other with the old data and the new data, and then having the bar plot of the new data colorised using a list comprehension, so something like:

clrs = ['red' if (x < 0) else 'green' for x in data["DELTA"] ]
plt.bar(data["DATE"], data["OLD VALUE"], color = "blue")
plt.bar(data["DATE"], data["NEW VALUE"], color = clrs)

This plots the old data first, and then the green and red new value data on top. But definitely is not what I'm looking for as it will simply just overwrite the old data completely, with only the new data sticking out a bit.

Any help appreciated, thank you.

CodePudding user response:

Plot first the old values, then the increases in green and then the decreases in red, using the enter image description here

CodePudding user response:

Very simple solution just turn the bars

plt.bar(data["DATE"], data["NEW VALUE"], color = clrs)
plt.bar(data["DATE"], data["OLD VALUE"], color = "blue")

So is the bigger bar ploted first and the smaller one gets plotted on top

  • Related