如何散景在轴上绘制英里和米的组合(例如7.5英里100m,7.5英里200m,8英里100m ....)

时间:2019-07-19 08:13:44

标签: python pandas bokeh

我需要使用散焦为X轴的距离绘制一些数据。但是,我提供的距离数据的格式为“ 7英里100米,7英里200m”等。

最好的方法是什么?

2 个答案:

答案 0 :(得分:0)

您应该将距离转换为米或英里。我举了一个小例子,其中所有距离都转换为米。您还可以添加一个悬停工具,以显示原始距离(组合英里/米)。

import pandas as pd
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import figure

data = {'day': [1, 2, 3, 4, 5], 'distance': ['7.5 Miles 100m', '7.5 Miles 200m', '7 Miles 500m', '5 Miles 300m', '8 Miles 5m'], 'color': Spectral5}
df = pd.DataFrame.from_dict(data)
#Split distance columns on spaces => distance in miles | 'miles' | distance in meters + 'm'
distancedf = df["distance"].str.split(" ", n = 2, expand = True)
#Convert miles column to floats and multiply by 1609.344 to get the distance in meters
distancedf[0] = distancedf[0].astype(float)
distancedf[0] *= 1609.344
#Remove all non numeric characters from column ('m') and convert to floats
distancedf[2].replace(regex=True,inplace=True,to_replace=r'\D',value=r'')
distancedf[2] = distancedf[2].astype(float)
#Add the sum of miles + meters to the first dataframe and use this dataframe for plotting
df['distanceinm'] = distancedf[0] + distancedf[2]

#Draw a plot
source = ColumnDataSource(df)
print(source.data)
p = figure(plot_height=250)
p.xaxis.axis_label = "Day"
p.yaxis.axis_label = "Distance in meters"
p.vbar(x='day', top='distanceinm', width=0.75, color='color', source=source)
show(p)

enter image description here

如果要同时以米和英里为单位显示距离,则必须添加一个附加轴。在以下示例中可以看到:

import pandas as pd
from bokeh.io import show
from bokeh.models import ColumnDataSource
from bokeh.palettes import Spectral5
from bokeh.plotting import figure
from bokeh.models import LinearAxis, Range1d
from bokeh.transform import dodge


data = {'day': [1, 2, 3, 4, 5], 'distance': ['7.5 Miles 100m', '7.5 Miles 200m', '7 Miles 500m', '5 Miles 300m', '8 Miles 5m'], 'color': Spectral5}
df = pd.DataFrame.from_dict(data)
#Split distance columns on spaces => distance in miles | 'miles' | distance in meters + 'm'
distancedf = df["distance"].str.split(" ", n = 2, expand = True)
#Convert miles column to floats and multiply by 1609.344 to get the distance in meters
df['distanceinmiles'] = distancedf[0].astype(float)
#Remove all non numeric characters from column ('m') and convert to floats
distancedf[2].replace(regex=True,inplace=True,to_replace=r'\D',value=r'')
df['distanceinmeters'] = distancedf[2].astype(float)

#Draw a plot
source = ColumnDataSource(df)
p = figure(plot_height=500, y_range=(0, df['distanceinmiles'].max()))
p.yaxis.axis_label = "Distance in Miles"
#Add extra Y axis
p.extra_y_ranges = {'meters': Range1d(start=0, end=df['distanceinmeters'].max())}
p.add_layout(LinearAxis(y_range_name="meters", axis_label='Distance in Meters'), 'right')
p.vbar(x=dodge('day', -0.21), top='distanceinmiles', width=0.4, color='color', source=source)
p.vbar(x=dodge('day', 0.21), top='distanceinmeters', width=0.4, color='color', source=source, y_range_name="meters")
p.xaxis.axis_label = "Day"
show(p)

enter image description here

(每个第一条都是以英里为单位的距离(在左侧Y轴上看到),第二条是以米为单位的距离(在右侧Y轴上看到)

答案 1 :(得分:0)

最后我弄清楚了,我只是将Miles(例如7.5)和Meters(例如400m)转换为Miles(例如7.75miles)。然后,我使用tickformatter将特定的点显示为米。感谢您的帮助