gpt4 book ai didi

python - Plotly.py : fill to zero, 正面/负面的不同颜色

转载 作者:行者123 更新时间:2023-12-05 06:24:58 34 4
gpt4 key购买 nike

使用 Plotly,我可以轻松绘制单条线并填充该线和 y == 0 之间的区域:

import plotly.graph_objects as go

fig = go.Figure()
fig.add_trace(go.Scatter(
x=[1, 2, 3, 4],
y=[-2, -1.5, 1, 2.5],
fill='tozeroy',
mode='lines',
))
fig.show()

enter image description here

如何将填充区域一分为二?特别是,用红色填充 y < 0和绿色在哪里 y > 0 .

我想保持线路连续。这意味着,我对只绘制两个单独的填充多边形不感兴趣。

请注意,该行不一定具有 y == 0 处的值.

最佳答案

我需要这样的图表,所以我写了一个函数。

def resid_fig(resid, tickvalues):
"""
resid: The y-axis points to be plotted. x-axis is assumed to be linearly increasing
tickvalues: The values you want to be displayed as ticklabels on x-axis. Works for hoverlabels too. Has to be
same length as `resid`. (This is necessary to ignore 'gaps' in graph where two polygons meet.)
"""
#Adjusting array with paddings to connect polygons at zero line on x-axis
index_array = []
start_digit = 0
split_array = np.split(resid,np.where(np.abs(np.diff(np.sign(resid)))==2)[0]+1)
split_array = [np.append(x,0) for x in split_array]
split_array = [np.insert(x,0,0) for x in split_array]
split_array[0] = np.delete(split_array[0],0)
split_array[-1] = np.delete(split_array[-1],-1)
for x in split_array:
index_array.append(np.arange(start_digit,start_digit+len(x)))
start_digit += len(x)-1

#Making an array for ticklabels
flat = []
for x in index_array:
for y in x:
flat.append(y)
flat_counter = Counter(flat)
none_indices = np.where([(flat_counter[x]>1) for x in flat_counter])[0]
custom_tickdata = []
neg_padding = 0
start_pos = 0
for y in range(len(flat)):
for x in range(start_pos,flat[-1]+1):
if x in none_indices:
custom_tickdata.append('')
break
custom_tickdata.append(tickvalues[x-neg_padding])
neg_padding +=1
start_pos = 1+x

#Making an array for hoverlabels
custom_hoverdata=[]
sublist = []
for x in custom_tickdata:
if x == '':
custom_hoverdata.append(sublist)
sublist = []
sublist.append(x)
continue
sublist.append(x)
sublist2 = sublist.copy()
custom_hoverdata.append(sublist2)

#Creating figure
fig = go.Figure()
idx = 0
for x,y in zip(split_array,index_array):
color = 'rgba(219,43,57,0.8)' if x[1]<0 else 'rgba(47,191,113,0.8)'
if (idx==0 and x[0] < 0):
color= 'rgba(219,43,57,0.8)'
fig.add_scatter(y=x, x=y, fill='tozeroy', fillcolor=color, line_color=color, customdata=custom_hoverdata[idx],
hovertemplate='%{customdata}<extra></extra>',legendgroup='mytrace',
showlegend=False if idx>0 else True)
idx += 1
fig.update_layout()
fig.update_xaxes(tickformat='', hoverformat='',tickmode = 'array',
tickvals = np.arange(index_array[-1][-1]+1),
ticktext = custom_tickdata)
fig.update_traces(mode='lines')
fig.show()

例子-

resid_fig([-2,-5,7,11,3,2,-1,1,-1,1], [1,2,3,4,5,6,7,8,9,10])

enter image description here

现在,对于警告-

  1. 它确实使用了单独的多边形,但我已将所有轨迹合并到一个 legendgroup 中,因此单击图例可打开或关闭所有这些轨迹。关于图例颜色,一种方法是将 fig.add_scatter 中的 showlegend=False if idx>0 中的 0 更改为 1 () 调用。然后它显示两个图例,红色和绿色仍然在同一个图例组中,所以它们仍然一起打开和关闭。

  2. 该函数的工作原理是,首先将连续的正值和负值分成数组,将 0 添加到每个数组的末尾和开头,以便多边形可以在 x 轴上相交。这意味着该图的缩放比例不高,但根据用例,它可能并不那么重要。这不会影响 hoverlabels 或 ticklabels,因为它们在这些聚合点上是空白的。

  3. 最重要的一点是,当传递的数组中的任何点为 0 时,图形无法按预期工作。我确定可以对其进行修改以适用于它,但我对它没有用,而且问题也没有要求它。

关于python - Plotly.py : fill to zero, 正面/负面的不同颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57421531/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com