我试图在Bokeh中绘制多行,但由于某种原因,它不起作用。 我已经读了一个有关熊猫的文件:
roads = pd.read_csv('path/file.csv')
,结果数据帧如下所示:
osm_id code fclass name ref oneway maxspeed layer bridge tunnel x y
0 4264947 5152 cycleway NaN NaN B 0 0 F F [4.9223553, 4.9223667, 4.9223853] [52.3654363, 52.3654277, 52.3654169]
1 4270144 5124 pedestrian Dapperplein NaN B 0 0 F F [4.9279354, 4.9280225, 4.9280546, 4.928041, 4.... [52.3619809, 52.362025, 52.3621167, 52.3622213...
2 4270151 5153 footway Zeeburgerstraat NaN B 0 0 F F [4.9261096, 4.9260732, 4.9258198] [52.3663263, 52.3662732, 52.3661229]
3 4270806 5152 cycleway Roomtuintjes NaN B 0 0 F F [4.9308652, 4.9308238, 4.9307548, 4.9304676, 4... [52.3659402, 52.3657291, 52.3655389, 52.364919...
4 4277440 5124 pedestrian Vondelpark NaN B 0 0 F F [4.881478, 4.8816828, 4.881693] [52.361566, 52.361699, 52.3617067]
当我尝试用散景绘制坐标时
source = ColumnDataSource(roads)
q = figure(title= "Roads", plot_width=900, plot_height=900)
q.multi_line('x', 'y', source = source, color='blue', line_width=0.5)
show(q)
它输出一个空图。
出了什么问题,有人可以帮助我吗?
谢谢!
答案 0 :(得分:1)
问题是列中的列表,这里是列表的字符串代表。因此,首先将两列都转换为列表:
import ast
roads = pd.read_csv('path/file.csv')
roads['x'] = roads['x'].apply(ast.literal_eval)
roads['y'] = roads['y'].apply(ast.literal_eval)
另一种解决方案:
import ast
conv = lambda x: ast.literal_eval(x)
roads = pd.read_csv('path/file.csv', converters={'x':conv, 'y':conv})
source = ColumnDataSource(roads)
q = figure(title= "Roads", plot_width=900, plot_height=900)
q.multi_line('x', 'y', source = source, color='blue', line_width=0.5)
show(q)