我在Flask模板中嵌入了散景图.该应用程序使用主样式表,而图表需要自己的样式.
主样式表泄漏到图表中并更改其外观.如何将不同的样式应用于模板中的不同部分,以便只有一种样式处于活动状态?
有关更多代码和图像,请参阅Bokeh邮件列表中的我的问题.
这是拉入图表对象的模板代码. resources,, script和div是图表库生成的对象元素.
{% extends "base.html" %}
{% block content %}
<frame>
<head>
<meta charset='utf-8' />
<meta http-equiv='content-type' content='text/html; charset=utf-8' />
{{ resources|indent(4)|safe }}
{{ script|indent(4)|safe }}
</head>
<body>
{{ div|indent(4)|safe }}
</body>
</frame>
{% endblock %}
Run Code Online (Sandbox Code Playgroud) 散景0.10的文档是可以的,并且有许多好例子.但是,我现在不知道如何告诉散景0.11使用容易记住的网址.我目前的尝试:
导入numpy作为np导入时间
from bokeh.client import push_session
from bokeh.plotting import figure, curdoc, output_server
x = np.random.rand(10)
y = np.random.rand(10)
p = figure()
r2 = p.line(x, y, color="navy", line_width=4)
# open a session to keep our local document in sync with server
session = push_session(curdoc())
session.show() # open the document in a browser
time.sleep(2)
while True:
x = np.random.rand(10)
y = np.random.rand(10)
r2.data_source.data["y"] = y
r2.data_source.data["x"] = x
time.sleep(2)
Run Code Online (Sandbox Code Playgroud)
来自Docs:
push_session(curdoc(),session_id = 'yeah')
然而,网址仍然有点笨拙:http://localhost:5006/?bokeh-session-id=yeah
有没有办法改变它http://localhost:5006/yeah? …
我使用ColumnDataSource在散点图中绘制了一堆数据,一列是X坐标,另一列是Y坐标.第三列的患者ID可能有重复.我想创建一个Tap派生工具,它将选择在患者ID列中共享相同值的所有其他x,y坐标.
'x' 'y' 'ID'
1 2 'p1'
2 3 'p1'
2 5 'p2'
0 1 'p2'
Run Code Online (Sandbox Code Playgroud)
所以基本上如果我在我的散景散点图中点击坐标(1,2),我会得到(1,2)和(2,3)点的选择,而其他所有选择都未被选中,就像你如何找到套索和盒子选择工具一样.
我有一个用于pd.Period索引的数据框,但后来需要使用该数据来绘制散景中的 x_axis。
df = return_data_with_multiindex()
df.reset_index(inplace=True, drop=False)
type(df["calDate"][0]) # returns pandas._period.Period
Run Code Online (Sandbox Code Playgroud)
不幸的是,散景不支持期间,所以我一直在思考如何最好地转换它。
TypeError: Object of type 'Period' is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
这个答案没有帮助,因为我仍然不断抛出类型错误:
AttributeError Traceback (most recent call last)
<ipython-input-287-8753a9594163> in <module>()
1 #pd.to_datetime(df["calDate"], format='%Y-%m')
----> 2 df["calDate"].to_timestamp()
3 df[["calDate","freq_hist_deseas","freq_futr","freq_tr12"]]
4 #type(df["calDate"][0])
C:\ANACONDA36\lib\site-packages\pandas\core\series.py in to_timestamp(self, freq, how, copy)
2709 new_values = new_values.copy()
2710
-> 2711 new_index = self.index.to_timestamp(freq=freq, how=how)
2712 return self._constructor(new_values,
2713 index=new_index).__finalize__(self)
AttributeError: 'RangeIndex' object has no attribute 'to_timestamp'
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
编辑:这是一个例子:
col1 col2 col3
i1 i2 …Run Code Online (Sandbox Code Playgroud) 对 csv 进行一些转换后,我有一个数据框:
ENTRYDATE | TRANSACTIONS
2017/05/01 5
2017/05/02 20
..
2018/02/05 15
Run Code Online (Sandbox Code Playgroud)
我将它转换为熊猫中的日期时间并创建了一个带有工具提示的线图
import pandas as pd
from bokeh.models import ColumnDataSource,DatetimeTickFormatter, NumeralTickFormatter, HoverTool
from bokeh.plotting import figure
from bokeh.io import curdoc
df=pd.read_csv(r'C:\Users\file.csv')
df2=df[['ENTRYDATE']]
df2['ENTRYDATE']=pd.to_datetime(df2['ENTRYDATE'],infer_datetime_format=True)
df2=(df2.groupby([df2['ENTRYDATE'].dt.date]).size().reset_index(name='Transactions'))
#print(df2)
#print(df2.info())
source=ColumnDataSource(data=df2)
#hovertool
p=figure(plot_width=800,plot_height=500)
hover=HoverTool(tooltips=[("Date","@ENTRYDATE"),("Transactions","@Transactions")],
formatters={"ENTRYDATE":"datetime"},
mode='vline')
p.add_tools(hover)
p.scatter(x="ENTRYDATE",y="Transactions",color='blue', source=source)
p.xaxis.formatter=DatetimeTickFormatter()
p.yaxis.formatter=NumeralTickFormatter(format="0")
curdoc().add_root(p)
Run Code Online (Sandbox Code Playgroud)
出于某种原因,显示 ENTRYDATE 的工具提示实际上并未显示日期(即2017-05-01),而是显示了一些长数字(即14962752000000)。有人可以告诉我如何编辑它以便工具提示以日期格式显示日期吗?
我需要创建一个如下所示的图表,一系列堆叠的条形图。有些值为正值,有些值为负值。我希望图表的显示方式就像 Excel 中的堆叠条形图一样,它将正值向上堆叠,将负值向下堆叠。有没有一种简单的方法可以对 N 个类别执行此操作?
这是一些示例数据:
df = pd.DataFrame(index=['06-11-2018', '06-12-2018', '06-13-2018', '06-14-2018', '06-15-2018'],
data={'A': [-378, -2347, 509, 987, 513],
'B': [-527, -2599, 765, 533, 670],
'C': [-2343, -2273, 2093, 2197, 1990],
'D': [-1845, -1853, 3325, 1306, 2160]})
Run Code Online (Sandbox Code Playgroud)
如何在Bokeh 0.12.11(以及可能的其他版本)中为悬停工具实现"工具提示"?
搜索"Bokeh hover工具提示"提供了大量文档结果,例如:https: //bokeh.pydata.org/en/latest/docs/user_guide/tools.html
但是,当我尝试通过以下示例实现Bokeh 0.12.11上的"工具提示"时:https://bokeh.pydata.org/en/latest/docs/gallery/elements.html
我收到以下错误:
AttributeError: unexpected attribute 'tooltips' to Figure, possible attributes are above, aspect_scale, etc.
我对 Bokeh 还是有点陌生,并且遇到了一个无法解决的问题。
我有一个散景图,在两个单独的图中可视化一些流数据。由于各种原因,绘图的用户可能希望在单击按钮时清除当前数据点的两个绘图。
清除数字的好方法是什么?我还没有找到好的解决方案。
我的代码看起来像这样:
#Defining plots
plot_data = ColumnDataSource(dict(x=[],y=[],z=[]))
p = figure(plot_height = 600, plot_width = 800,
x_axis_label = 'X',
y_axis_label = 'Y')
p2 = figure(plot_height = 600, plot_width = 800,
x_axis_label = 'X',
y_axis_label = 'Z')
doc = curdoc()
Run Code Online (Sandbox Code Playgroud)
数据源在异步循环中更新:
async def loop():
while True:
data = await socket.recv_pyobj()
new_data = get_last_data(data)
#update ColumnDataSource
doc.add_next_tick_callback(partial(update,new_data))
doc.add_root(column(gridplot([p,p2], plot_width=1000)))
try:
testloop = IOLoop.current()
testloop.spawn_callback(loop)
except KeyboardInterrupt:
testloop.close()
Run Code Online (Sandbox Code Playgroud)
ColumnDataSource当流中出现新数据点(解析为数据帧)时,将通过以下函数进行更新
def update(new_data):
input_data = dict(x=new_data['x'], y=new_data['y'], z=new_data['z'])
plot_data.stream(input_data, rollover=500)
Run Code Online (Sandbox Code Playgroud)
我通过单击按钮清除数字的最初想法如下: …
bokeh ×8
python ×6
pandas ×2
charts ×1
css ×1
flask ×1
html ×1
matplotlib ×1
python-3.x ×1
seaborn ×1