import pandas as pd
import plotly.graph_objs as go
import plotly.express as px
data = [[-0.12258064516129029,
-0.004118444374934793,
-0.0019027849852966516,
-0.0005512679162073629,
0.0,
0.000688705234159765,
0.0015693659761456594,
0.002682763246143497,
0.004020677771395803,
0.006778868630201008,
0.04581165187225787],
[-0.10469770381293453,
-0.0025438596491228617,
-0.0006713662302786027,
-2.3809523808886775e-06,
0.0006414368184733667,
0.0013561829614104233,
0.002134646962233211,
0.003375843960990242,
0.0048455481526346635,
0.007221182134260391,
0.0503304524656837],
[-0.10066399574146195,
-0.0038356842365749578,
-0.0020833333333334122,
-0.000994530084535036,
-0.00010378288620211837,
0.0001390240511608355,
0.00099814629972918,
0.0018181818181817794,
0.0029944838455476957,
0.0054733118873015844,
0.049994048327580094],
[-0.1072961373390558,
-0.007906041495293017,
-0.005279164514833396,
-0.003605459696111269,
-0.0024149034038638347,
-0.001434720229555291,
-0.0004974762182100464,
0.00018744142455478932,
0.0014705882352940864,
0.0031733543141882494,
0.0480364656381487],
[-0.12765957446808504,
-0.005706521739130266,
-0.0035444234404536524,
-0.002334630350194503,
-0.0013410818059902162,
-0.0004737091425863577,
4.282105082848753e-05,
0.0012012012012013891,
0.002486572508454346,
0.004981320049813272,
0.06135190298955461]]
colors = ['#df1010', '#e43232', '#e85454', '#ed7777', '#f29898',
'#afd6a7', '#8fc383', '#6db05e', '#4d9e3a', '#2b8b15']
# create data for positive and negative level bers
plevel, nlevel, limit = [], [], 0.02
for d in range(len(data[0])):
plevel.append([max(min(data[i][d], limit), 0.0) for i in range(len(data))])
nlevel.append([min(max(data[i][d], -limit), 0.0) for i in range(len(data))])
x = list(range(1, len(data)+1))
print(plevel)
print(nlevel)
fig = go.Figure()
# plot the positive and negative bars with correct y and base settings
for i in range(len(plevel)-1):
fig.add_trace(go.Bar(x=x, y=nlevel[i], base=nlevel[i+1]))
fig.add_trace(go.Bar(x=x, y=plevel[i+1], base=plevel[i]))
# hard-set the coloring for all 20 bars to 0-9,0-9
for c in range(len(colors)):
fig.data[c].marker.color = [colors[c] for _ in range(len(nlevel))]
fig.data[c+len(colors)].marker.color = [colors[c] for _ in range(len(nlevel))]
fig.update_layout(barmode='overlay', yaxis={'tickformat': '.1%'})
fig.show()
here is how it looks. The gaps are gone but there’s still some funky play in the middle that probably has to do with the middle transition. All ten bars should be colored from the deepest red at the bottom to the deepest green at the top.
I am wondering why the upper and lower limits of each bar are not hard set to 0.02 and -0.02 as the data is clearly set to, and the output of those prints is:
[[0.0, 0.0, 0.0, 0.0, 0.0], ..., [0.02, 0.02, 0.02, 0.02, 0.02]]
[[-0.02, -0.02, -0.02, -0.02, -0.02], ..., [0.0, 0.0, 0.0, 0.0, 0.0]]
Is these values are fluctuating, how do I know if any others are correct and suspect they also might not be? Tinker with that limit
var setting and please explain why the limits are always random floats close to but not equal to that value? Mysterious…
I think we are hacking out a crazy way to make this work under the circumstances, but you now can clearly see the requirement of:
There must be a way to consistently graph a bar from level X to level Y whether both values are below zero, both values are above zero, or one value is above zero and the other value is below zero.
What is also trippy is if you comment out one or the other fig.add_trace(go.Bar
call and the second color definition line, it seems to draw the top or bottom half of the chart perfectly. But when you put the two together, you get that funky play in the middle?