最初的目标
我在Bokeh中有一个烛台情节,本质上是,但使用我的自定义数据。
我想要的只是该情节背后的灰色渐变背景。
我尝试的
bokeh没有 Backick_gradient_fill
选项,因此我尝试使用 image> image_url
我在网上找到了一个渐变,这是不起作用的,因为Firefox不允许第三聚会图像加载。无论如何,此选项很糟糕,因为它很慢,并且依赖于Google附近的一些URL。
我尝试制作自己的渐变numpy数组,然后添加它们为 > Image 这是不起作用的,因为图像坐标基本上是在我的情节上硬编码的,但是我的烛台是动态的,并且正在实时加载。
我终于以为我会做一个渐变 div 使用CSS ,将烛台图填充到透明,然后彼此叠加。我尝试使用 GridPlot
,列
, row
,这都不能真正允许您从我的内容中控制其中元素的偏移看到。所以现在我真的不知道该怎么办...
如何将两者挤在一起?
我现在拥有的:
我的代码当前会产生以下方式:
我只是希望这两个基本上都在彼此之上。我该怎么办?
最小可复制示例:
为此的MRE与 ,但增加了Div。要运行,只需将此代码放在python文件中,然后运行 bokeh sampledata
要下载示例数据,然后 bokeh serve -show yourfilename.py
import pandas as pd
from bokeh.layouts import gridplot
from bokeh.models import Div
from bokeh.plotting import curdoc
from bokeh.plotting import figure
from bokeh.sampledata.stocks import MSFT
from math import pi
df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])
inc = df.close > df.open
dec = df.open > df.close
w = 12 * 60 * 60 * 1000 # half day in ms
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
p = figure(x_axis_type="datetime", tools=TOOLS, width=1000, title="MSFT Candlestick")
p.xaxis.major_label_orientation = pi / 4
p.grid.grid_line_alpha = 0.3
div = Div(style={"background": "linear-gradient(rgb(40,40,40), rgb(10,10,10))",
"height": "1000px", "width": "1000px"}, sizing_mode="scale_both")
p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")
curdoc().add_root(gridplot([[p, div]], toolbar_location="left"))
Initial Goal
I have a candlestick plot in Bokeh that is essentially a clone of the MSFT candlesticks example but with my custom data.
All I want is a grey-black gradient background behind that plot.
What I've Tried
Bokeh doesn't have background_gradient_fill
option, so I tried using an image_url
I found online of a gradient, that didn't work because firefox doesn't allow third party image loading. This option sucks anyway because it's slow and relies on some url off google.
I tried making my own gradient numpy array and them adding that as an image
that didn't work because the image coords are basically hard-coded onto my plot, but my candlesticks are dynamic and are being loaded in live.
I finally thought I'd make a Gradient Div
styled using CSS, set the candlestick plot fill to transparent, then overlay on top of each other. I tried doing that using gridplot
, column
, row
and neither really allows you to control the offset of the elements within it from what I've seen. So now I don't really know what to do...
How do I squash the two together?
What I Have Now:
My code currently produces this:
I just want both of these on top of each other basically. How do I do so?
Minimal Repliclable Example:
The MRE for this is the same as the MSFT candlesticks example but with the added div. To run, just place this code in a python file and run bokeh sampledata
to download sample data then bokeh serve --show yourfilename.py
import pandas as pd
from bokeh.layouts import gridplot
from bokeh.models import Div
from bokeh.plotting import curdoc
from bokeh.plotting import figure
from bokeh.sampledata.stocks import MSFT
from math import pi
df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])
inc = df.close > df.open
dec = df.open > df.close
w = 12 * 60 * 60 * 1000 # half day in ms
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
p = figure(x_axis_type="datetime", tools=TOOLS, width=1000, title="MSFT Candlestick")
p.xaxis.major_label_orientation = pi / 4
p.grid.grid_line_alpha = 0.3
div = Div(style={"background": "linear-gradient(rgb(40,40,40), rgb(10,10,10))",
"height": "1000px", "width": "1000px"}, sizing_mode="scale_both")
p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")
curdoc().add_root(gridplot([[p, div]], toolbar_location="left"))
发布评论
评论(1)
如果您想要梯度背景,则可以手动构建它。您可以根据X和Y轴的值生成图像,而不是将这些值映射到线性颜色图。
上面的代码给出以下结果:
If you want a gradient background, you can build it manually. You can generate an image based on the values of X and Y axis and than map these values to a Linear Colour Map.
The code above gives the following result: