python - How to add the sliders values in Bokeh for python
问题描述
I am not able to figure it how to add the income bar in the sliders and what does circle mean inside. Can anyone help me solving this. I am now able add title on the top of slider. I have attached link the reference below. This is what I have Original .
Needed this type enter image description here
necessary imports
from bokeh.plotting import figure, show, output_file
from bokeh.tile_providers import CARTODBPOSITRON
import pandas as pd
import numpy as np
import math
from ast import literal_eval
from bokeh.palettes import Viridis5
from bokeh.models import ColumnDataSource,ColorBar,BasicTicker
from bokeh.models.mappers import ColorMapper, LinearColorMapper
#function to convert latitude and longitude into mercator projection mapping
def merc(Coords):
Coordinates = literal_eval(Coords)
lat = Coordinates[0]
lon = Coordinates[1]
r_major = 6378137.000
x = r_major * math.radians(lon)
scale = x/lon
y = 180.0/math.pi * math.log(math.tan(math.pi/4.0 + lat * (math.pi/180.0)/2.0)) * scale
return (x, y)
#supporting function
def make_tuple_str(x, y):
t = (x, y)
return str(t)
#read with pandas
df = pd.read_csv('housing.csv')
#converting latitude and longitudes of corners of california into mercator range
range0 = merc('(32.080577, -114.052642)')
range1 = merc('(42.356802, -124.753326)')
x_range = (range0[0],range1[0])
y_range = (range0[1], range1[1])
#now convert DataFrame longitude and latitude column into mercator coordinates
df['coords'] = df.apply(lambda x: make_tuple_str(x['latitude'], x['longitude']), axis = 1)
df['coords_latitude'] = df['coords'].apply(lambda x: merc(x)[0])
df['coords_longitude'] = df['coords'].apply(lambda x: merc(x)[1])
#prepare data
source = ColumnDataSource(
data = dict(
lat = df['coords_latitude'].tolist(),
lon = df['coords_longitude'].tolist(),
size = df.median_income.tolist(),
color = df.median_house_value.tolist()
)
)
#initiate figure
p = figure(x_range = x_range, y_range = y_range, x_axis_type = "mercator", y_axis_type = "mercator")
p.add_tile(CARTODBPOSITRON)
#color palette
color_mapper = LinearColorMapper(palette = Viridis5)
#add a glyph
p.circle(x = 'lat', y = 'lon',
size = 'size',
color = {'field':'color',
'transform':color_mapper},
source = source
)
#put a colorbar to support
color_bar = ColorBar(color_mapper=color_mapper, ticker=BasicTicker(),
label_standoff=12, border_line_color=None, location=(0,0))
#layout
p.add_layout(color_bar, 'right')
#show it in web browser
output_file('example.html')
show(p)
The link to the main code is :
解决方案
推荐阅读
- javascript - 为什么我的 Redux 道具对象返回未定义?
- php - 用户池客户端 {id} 不存在
- java - 我的代码有什么问题。Decryption.txt 不接受 java 文件操作中的任何消息。?
- javascript - 如何在 Chrome 浏览器中使用 javascript 验证有效的 Date 对象
- php - 我想用 php (.htaccess) 重写一个 URL
- python - 使用 MongoDB 更改流时代码不运行
- c++ - 16X32 RGB Led矩阵,矩阵多重定义
- c# - c# linq 包含参数值
- amazon-web-services - 是否可以从雅典娜查询中删除存储在 S3 存储桶中的整个表?
- postgresql - k8s docker容器挂载主机,但输出日志文件失败