Python:如何使用 plotly 制作阴影区域或交替背景颜色?

2022-01-21 00:00:00 python plotly plotly-python

问题描述

仅使用

如何设置它,以便您可以在下面的图中使用交替的 bakcground 颜色,就像

感谢您的任何建议!

解决方案

正如问题中所建议的,可能的解决方案可能在于 vspan 函数.然而,使用 hspan 为 y 轴添加多个阴影区域似乎比使用 vspan 和 x 轴的情况要容易得多.后者需要更多的调整.在我建议的解决方案之后可以找到更多详细信息.

<小时>

下面的图是由下面的代码片段和函数 multiShades 生成的:

剧情:

片段:

### 问题设置###导入情节将袖扣导入为 cf从 plotly.offline 导入 download_plotlyjs, init_notebook_mode, plot, iplot将熊猫导入为 pd将 numpy 导入为 np从 IPython.display 导入 HTML从 IPython.core.display 导入显示,HTML导入副本# 设置init_notebook_mode(连接=真)np.random.seed(123)cf.set_config_file(主题='珍珠')# 使用袖扣的随机数据df = cf.datagen.lines()fig = df.iplot(asFigure=True, kind='scatter',xTitle='日期',yTitle='回报',title='回报',vspan={'x0':'2015-01-11','x1':'2015-02-22','color':'rgba(30,30,30,0.3)','fill':True,'不透明度':.4})### 回答 ###xStart = ['2015-01-11', '2015-02-08', '2015-03-08', '2015-04-05']xStop = ['2015-01-25', '2015-02-22', '2015-03-22', '2015-04-10']def multiShades(绘图,x0,x1):""" 在绘图中为指定日期添加阴影区域.使用 rgba(0,0,0,0) 将区域的线条设置为透明"""# 获取开始和结束日期x0 = x开始x1 = x停止# 从 vspan() 生成的元组中获取字典xElem = fig['layout']['shapes'][0]# 字典/形状的容器(列表)shp_lst=[]# 根据 x0 和 X1 制作字典# 并编辑这些字典的元素对于范围内的我(0,len(x0)):shp_lst.append(copy.deepcopy(xElem))shp_lst[i]['x0'] = x0[i]shp_lst[i]['x1'] = x1[i]shp_lst[i]['line']['color'] = 'rgba(0,0,0,0)'# 用多个新形状替换 fig 中的形状fig['layout']['shapes']= tuple(shp_lst)返回(图)无花果=多阴影(情节=无花果,x0=xStart,x1=xStop)iplot(无花果)

一些细节:

函数 vspan 用以下形式的字典填充"元组 fig['layout']['shapes']:

{'fillcolor': 'rgba(187, 187, 187, 0.4)','line': {'color': '#BBBBBB', 'dash': 'solid', 'width': 1},'类型':'矩形','x0': '2015-01-11','x1': '2015-02-22','外部参照':'x','y0': 0,'y1': 1,'yref': '纸'}

我的函数只是获取该字典,制作多个副本,根据函数参数编辑这些副本,然后用函数中的新元组替换原始元组.

挑战:

当添加更多形状时,这种方法可能会有点棘手.此外,必须对日期进行硬编码 - 至少在有人找到 如何检索主要刻度和网格线的值?

Using only these few lines of code from plot.ly will give you the plot below in a jupyter notebook:

Snippet 1:

import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
init_notebook_mode(connected=True)

iplot(cf.datagen.lines().iplot(asFigure=True,
                               kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))

Plot 1:

How can you set it up so you can have alternating bakcground colors in the plot below like it was shown in this post using matplotlib?

Here's a link that explains how to add shaded areas like this:

Snippet 2:

df.iplot(vspan={'x0':'2015-02-15','x1':'2015-03-15','color':'rgba(30,30,30,0.3)','fill':True,'opacity':.4}, 
         filename='cufflinks/custom-regions')

Plot 2:

Thank you for any suggestions!

解决方案

As suggested in the question, a possible solution could lie in the vspan function. However, it seemed much easier to add multiple shaded areas for the y-axis using hspan, than the case was with vspan and the x-axis. The latter needed a little more tweaking. More details can be found after my suggested solution.


The following plot is produced by the snippet and function multiShades below:

Plot:

Snippet:

### Setup from the question ###

import plotly
import cufflinks as cf
from plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplot
import pandas as pd
import numpy as np
from IPython.display import HTML
from IPython.core.display import display, HTML
import copy

# setup
init_notebook_mode(connected=True)
np.random.seed(123)
cf.set_config_file(theme='pearl')

# Random data using cufflinks
df = cf.datagen.lines()

fig = df.iplot(asFigure=True, kind='scatter',
               xTitle='Dates',yTitle='Returns',title='Returns',
               vspan={'x0':'2015-01-11','x1':'2015-02-22','color':'rgba(30,30,30,0.3)','fill':True,'opacity':.4})

### ANSWER ###

xStart = ['2015-01-11', '2015-02-08', '2015-03-08', '2015-04-05']
xStop = ['2015-01-25', '2015-02-22', '2015-03-22', '2015-04-10']

def multiShades(plot, x0, x1):
    """ Adds shaded areas for specified dates in a plotly plot.
        The lines of the areas are set to transparent using rgba(0,0,0,0)
    """
    # get start and end dates
    x0 = xStart
    x1 = xStop

    # get dict from tuple made by vspan()
    xElem = fig['layout']['shapes'][0]

    # container (list) for dicts / shapes
    shp_lst=[]

    # make dicts according to x0 and X1
    # and edit elements of those dicts
    for i in range(0,len(x0)):
        shp_lst.append(copy.deepcopy(xElem))
        shp_lst[i]['x0'] = x0[i]
        shp_lst[i]['x1'] = x1[i]
        shp_lst[i]['line']['color'] = 'rgba(0,0,0,0)'

    # replace shape in fig with multiple new shapes
    fig['layout']['shapes']= tuple(shp_lst)
    return(fig)

fig = multiShades(plot=fig, x0=xStart, x1=xStop)

iplot(fig)

Some details:

The function vspan 'fills' the tuple fig['layout']['shapes'] with a dictionary of the form:

{'fillcolor': 'rgba(187, 187, 187, 0.4)',
 'line': {'color': '#BBBBBB', 'dash': 'solid', 'width': 1},
 'type': 'rect',
 'x0': '2015-01-11',
 'x1': '2015-02-22',
 'xref': 'x',
 'y0': 0,
 'y1': 1,
 'yref': 'paper'}

My function simply takes that dictionary, makes a number of copies, edits those copies according to the function arguments, and replaces the original tuple with a new tuple from the function.

Challenges:

This approach might get a bit tricky when more shapes are added. In addition, the dates have to be hard-coded - atleast until someone finds an answer to How to retrieve values for major ticks and gridlines?

相关文章