我正在尝试在下面的代码中绘制列表“w”的长度的折线图。当我使用来自散景的spectr11时,我得到的只是图表上的11行,其中列表包含24个参数。是否有任何其他调色板允许我绘制列表“w”
中的所有行#Import the library
import pandas
import bokeh
import MySQLdb
from bokeh.plotting import figure, output_file, show
from bokeh.palettes import Spectral11
w=['F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','G1','G2','G3','G4','G5','G6','G7','G8','G9','G10','G11','G12']
p = figure(plot_width=800, plot_height=500, x_axis_type="datetime")
p.title.text = 'Click on legend entries to hide the corresponding lines'
# Open database connection
db = MySQLdb.connect("localhost","user","password","db" )
在下面的for循环中,只形成了11个数据帧,最终绘制了这11行。
for name, color in zip(w, Spectral11):
stmnt='select date_time,col1,w,test_value from db where w="%s"'%(name)
df=pandas.read_sql(stmnt,con=db)
p.line(df['date_time'], df['test_value'], line_width=2, color=color, alpha=0.8, legend=name)
p.legend.location = "top_left"
p.legend.click_policy="hide"
output_file("interactive_legend.html", title="interactive_legend.py example")
show(p)
下面的
答案 0 :(得分:3)
问题是zip
在较短列表的末尾被截断。
In [1]: from bokeh.palettes import Spectral11
In [2]: w=['F1','F2','F3','F4','F5','F6','F7','F8','F9','F10','F11','F12','G1','G2','G3','G4','G5','G6','G7','G8'
...: ,'G9','G10','G11','G12']
In [3]: for name, color in zip(w, Spectral11):
...: print(name, color)
...:
F1 #5e4fa2
F2 #3288bd
F3 #66c2a5
F4 #abdda4
F5 #e6f598
F6 #ffffbf
F7 #fee08b
F8 #fdae61
F9 #f46d43
F10 #d53e4f
F11 #9e0142
选择比w
列表更长的调色板可以解决此问题(如@PabloReyes所述)。您也可以使用itertools.cycle
。
In [4]: import itertools
In [5]: for name, color in zip(w, itertools.cycle(Spectral11)):
...: print(name, color)
...:
F1 #5e4fa2
F2 #3288bd
F3 #66c2a5
F4 #abdda4
F5 #e6f598
F6 #ffffbf
F7 #fee08b
F8 #fdae61
F9 #f46d43
F10 #d53e4f
F11 #9e0142
F12 #5e4fa2
G1 #3288bd
G2 #66c2a5
G3 #abdda4
G4 #e6f598
G5 #ffffbf
G6 #fee08b
G7 #fdae61
G8 #f46d43
G9 #d53e4f
G10 #9e0142
G11 #5e4fa2
G12 #3288bd
您可能还想使用line_dash
参数。
In [6]: import bokeh.plotting
...: import numpy as np
...: import pandas as pd
...:
...: bokeh.plotting.output_file('cycle_demo.html')
...: lines = np.random.random((100, len(w))) + np.arange(24)
...: df = pd.DataFrame(lines)
...: df.columns = w
...:
...: line_dash_styles = [[10, 0], [20, 1], [10, 1], [5, 1]]
...: p = bokeh.plotting.figure()
...: for name, color, line_dash in zip(w, itertools.cycle(Spectral11), itertools.cycle(line_dash_styles)):
...: p.line(np.arange(100), df[name], color=color, legend=name, line_dash=line_dash)
...:
...: p.legend.location = "top_left"
...: bokeh.plotting.show(p)
...:
答案 1 :(得分:2)
有不同类型的调色板,颜色数不同。检查:http://bokeh.pydata.org/en/latest/docs/reference/palettes.html。你可以选择其中之一。
我建议那些functions
,你可以从像viridis或inferno这样的大调色板中指定你想要的颜色数。
from bokeh.palettes import inferno
mypalette = inferno(24)
在Jupyter笔记本中使用随机行测试:
import bokeh
import bokeh.plotting
import numpy as np
bokeh.io.output_notebook()
lines = np.random.random((24,100))
p = bokeh.plotting.figure()
mypalette24 = bokeh.palettes.inferno(24)
for i,color in enumerate(mypalette24):
p.line(np.arange(100),i+lines[i,:],color=color,legend=str(i+1))
p.legend.location = "top_left"
bokeh.io.show(p)