亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python:如何使用plotly制作陰影區域或交替背景顏色?

Python:如何使用plotly制作陰影區域或交替背景顏色?

慕神8447489 2021-11-30 15:48:17
僅使用plot.ly 中的這幾行代碼將在 jupyter notebook 中為您提供以下圖:片段 1:import plotlyimport cufflinks as cffrom plotly.offline import download_plotlyjs, init_notebook_mode, plot, iplotinit_notebook_mode(connected=True)iplot(cf.datagen.lines().iplot(asFigure=True,                               kind='scatter',xTitle='Dates',yTitle='Returns',title='Returns'))情節 1:您如何設置它以便您可以在下面的圖中使用交替的背景顏色,就像這篇文章中使用matplotlib顯示的那樣?這是一個鏈接,解釋了如何添加這樣的陰影區域:片段 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')情節 2:
查看完整描述

2 回答

?
慕桂英3389331

TA貢獻2036條經驗 獲得超8個贊

正如問題中所建議的,一個可能的解決方案可能在于vspan函數。但是,使用hspan,為 y 軸添加多個陰影區域似乎比使用vspan和 x 軸容易得多。后者需要更多的調整。在我建議的解決方案之后可以找到更多詳細信息。


下圖由以下代碼段和函數生成multiShades

陰謀:

http://img1.sycdn.imooc.com//61a5d7860001fd1b07880458.jpg

片段:


### 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)

一些細節:


該函數用以下形式的字典vspan“填充”元組fig['layout']['shapes']:


{'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'}

我的函數只是獲取該字典,制作多個副本,根據函數參數編輯這些副本,然后用函數中的新元組替換原始元組。


查看完整回答
反對 回復 2021-11-30
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

您現在可以使用plotly 4.12 中添加的fig.add_vrect()來執行此操作。例如,要添加對應于白天時間的陰影區域,有一個列表,早上,指定每天的早上時間,然后:


for morning in mornings:

    fig.add_vrect(

        x0=morning,

        x1=morning + timedelta(hours=12),

        fillcolor="white",

        opacity=0.1,

        line_width=0,

    )


查看完整回答
反對 回復 2021-11-30
  • 2 回答
  • 0 關注
  • 469 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號