diff --git a/pymove/tests/test_visualization_matplotlib.py b/pymove/tests/test_visualization_matplotlib.py index c5075097..897ebded 100644 --- a/pymove/tests/test_visualization_matplotlib.py +++ b/pymove/tests/test_visualization_matplotlib.py @@ -50,7 +50,6 @@ def test_show_object_id_by_date(tmpdir): mpl.show_object_id_by_date( move_data=move_df, - create_features=False, name=filename_write_default ) @@ -64,22 +63,6 @@ def test_show_object_id_by_date(tmpdir): in_decorator=False ) - assert(HOUR not in move_df) - assert(DATE not in move_df) - assert(PERIOD not in move_df) - assert(DAY not in move_df) - - mpl.show_object_id_by_date( - move_data=move_df, - create_features=True, - name=filename_write_default - ) - - assert(DATE in move_df) - assert(HOUR in move_df) - assert(PERIOD in move_df) - assert(DAY in move_df) - def test_plot_traj_by_id(tmpdir): move_df = _default_move_df() diff --git a/pymove/visualization/folium.py b/pymove/visualization/folium.py index 2755e34e..cc99da4d 100644 --- a/pymove/visualization/folium.py +++ b/pymove/visualization/folium.py @@ -1169,13 +1169,6 @@ def plot_trajectory_by_period( 3 39.984211 116.319389 2008-10-23 05:53:16 1 4 39.984217 116.319422 2008-10-23 05:53:21 1 >>> plot_trajectory_by_period(move_df, period='Early morning') - >>> move_df.head() - lat lon datetime id period - 0 39.984094 116.319236 2008-10-23 05:53:05 1 Early morning - 1 39.984198 116.319322 2008-10-23 05:53:06 1 Early morning - 2 39.984224 116.319402 2008-10-23 05:53:11 1 Early morning - 3 39.984211 116.319389 2008-10-23 05:53:16 1 Early morning - 4 39.984217 116.319422 2008-10-23 05:53:21 1 Early morning """ if base_map is None: base_map = create_base_map( @@ -1186,6 +1179,7 @@ def plot_trajectory_by_period( default_zoom_start=zoom_start, ) + columns = move_data.columns if PERIOD not in move_data: move_data.generate_time_of_day_features() @@ -1194,6 +1188,8 @@ def plot_trajectory_by_period( _add_trajectories_to_folium_map( mv_df, items, base_map, legend, save_as_html, filename ) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) return base_map @@ -1278,13 +1274,6 @@ def plot_trajectory_by_day_week( 3 39.984211 116.319389 2008-10-23 05:53:16 1 4 39.984217 116.319422 2008-10-23 05:53:21 1 >>> plot_trajectory_by_day_week(move_df, day_week='Friday') - >>> move_df.head() - lat lon datetime id day - 0 39.984094 116.319236 2008-10-23 05:53:05 1 Thursday - 1 39.984198 116.319322 2008-10-23 05:53:06 1 Thursday - 2 39.984224 116.319402 2008-10-23 05:53:11 1 Thursday - 3 39.984211 116.319389 2008-10-23 05:53:16 1 Thursday - 4 39.984217 116.319422 2008-10-23 05:53:21 1 Thursday """ if base_map is None: base_map = create_base_map( @@ -1295,6 +1284,7 @@ def plot_trajectory_by_day_week( default_zoom_start=zoom_start, ) + columns = move_data.columns if DAY not in move_data: move_data.generate_day_of_the_week_features() @@ -1303,6 +1293,8 @@ def plot_trajectory_by_day_week( _add_trajectories_to_folium_map( mv_df, items, base_map, legend, save_as_html, filename ) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) return base_map @@ -1394,13 +1386,6 @@ def plot_trajectory_by_date( >>> start_date='2008-10-23 05:53:05', >>> end_date='2008-10-23 23:43:56' >>> ) - >>> move_df.head() - lat lon datetime id date - 0 39.984094 116.319236 2008-10-23 05:53:05 1 2008-10-23 - 1 39.984198 116.319322 2008-10-23 05:53:06 1 2008-10-23 - 2 39.984224 116.319402 2008-10-23 05:53:11 1 2008-10-23 - 3 39.984211 116.319389 2008-10-23 05:53:16 1 2008-10-23 - 4 39.984217 116.319422 2008-10-23 05:53:21 1 2008-10-23 """ if base_map is None: base_map = create_base_map( @@ -1417,6 +1402,7 @@ def plot_trajectory_by_date( if isinstance(end_date, str): end_date = str_to_datetime(end_date).date() + columns = move_data.columns if DATE not in move_data: move_data.generate_date_features() @@ -1425,6 +1411,8 @@ def plot_trajectory_by_date( _add_trajectories_to_folium_map( mv_df, items, base_map, legend, save_as_html, filename ) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) return base_map @@ -1511,13 +1499,7 @@ def plot_trajectory_by_hour( 2 39.984224 116.319402 2008-10-23 05:53:11 1 3 39.984211 116.319389 2008-10-23 05:53:16 1 4 39.984217 116.319422 2008-10-23 05:53:21 1 - >>> plot_trajectory_by_hour(move_df, start_hour=4,end_hour=6) - lat lon datetime id hour - 0 39.984094 116.319236 2008-10-23 05:53:05 1 5 - 1 39.984198 116.319322 2008-10-23 05:53:06 1 5 - 2 39.984224 116.319402 2008-10-23 05:53:11 1 5 - 3 39.984211 116.319389 2008-10-23 05:53:16 1 5 - 4 39.984217 116.319422 2008-10-23 05:53:21 1 5 + >>> plot_trajectory_by_hour(move_df, start_hour=4, end_hour=6) """ if base_map is None: base_map = create_base_map( @@ -1528,6 +1510,7 @@ def plot_trajectory_by_hour( default_zoom_start=zoom_start, ) + columns = move_data.columns if HOUR not in move_data: move_data.generate_hour_features() @@ -1536,6 +1519,8 @@ def plot_trajectory_by_hour( _add_trajectories_to_folium_map( mv_df, items, base_map, legend, save_as_html, filename ) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) return base_map @@ -1620,19 +1605,6 @@ def plot_stops( 3 39.984211 116.319389 2008-10-23 05:53:16 1 4 39.984217 116.319422 2008-10-23 05:53:21 1 >>> plot_stops(move_df) - >>> move_df.head() - lat lon datetime id \ - dist_to_prev dist_to_next dist_prev_to_next situation - 0 39.984094 116.319236 2008-10-23 05:53:05 1 \ - NaN 13.690153 NaN nan - 1 39.984198 116.319322 2008-10-23 05:53:06 1 \ - 13.690153 7.403788 20.223428 move - 2 39.984224 116.319402 2008-10-23 05:53:11 1 \ - 7.403788 1.821083 5.888579 move - 3 39.984211 116.319389 2008-10-23 05:53:16 1 \ - 1.821083 2.889671 1.873356 move - 4 39.984217 116.319422 2008-10-23 05:53:21 1 \ - 2.889671 66.555997 68.727260 move """ if base_map is None: base_map = create_base_map( @@ -1643,6 +1615,7 @@ def plot_stops( default_zoom_start=zoom_start, ) + columns = move_data.columns if SITUATION not in move_data: move_data.generate_move_and_stop_by_radius(radius=radius) @@ -1669,6 +1642,8 @@ def plot_stops( if save_as_html: base_map.save(outfile=filename) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) return base_map diff --git a/pymove/visualization/matplotlib.py b/pymove/visualization/matplotlib.py index a2105cdb..8b526db7 100644 --- a/pymove/visualization/matplotlib.py +++ b/pymove/visualization/matplotlib.py @@ -7,7 +7,7 @@ plot_all_features plot_coords, plot_bounds, -plot_line, +plot_line """ @@ -37,7 +37,6 @@ def show_object_id_by_date( move_data: Union['PandasMoveDataFrame', 'DaskMoveDataFrame'], - create_features: bool = True, kind: Optional[List] = None, figsize: Tuple[float, float] = (21, 9), return_fig: bool = True, @@ -56,9 +55,6 @@ def show_object_id_by_date( ---------- move_data : pymove.core.MoveDataFrameAbstract subclass. Input trajectory data. - create_features : bool, optional - Represents whether or not to delete features created for viewing, - by default True. kind: list, optional Determines the kinds of each plot, by default None figsize : tuple, optional @@ -79,12 +75,24 @@ def show_object_id_by_date( ---------- https://matplotlib.org/3.1.1/api/_as_gen/matplotlib.pyplot.plot.html + Examples + -------- + >>> from pymove.visualization.matplotlib import show_object_id_by_date + >>> move_df.head() + lat lon datetime id + 0 39.984094 116.319236 2008-10-23 05:53:05 1 + 1 39.984198 116.319322 2008-10-23 05:53:06 1 + 2 39.984224 116.319402 2008-10-23 05:53:11 1 + 3 39.984211 116.319389 2008-10-23 05:53:16 2 + 4 39.984217 116.319422 2008-10-23 05:53:21 2 + >>> show_object_id_by_date(move_df) """ if kind is None: kind = ['bar', 'bar', 'line', 'line'] fig, ax = plt.subplots(2, 2, figsize=figsize) + columns = move_data.columns move_data.generate_date_features() move_data.generate_hour_features() move_data.generate_time_of_day_features() @@ -108,12 +116,12 @@ def show_object_id_by_date( subplots=True, kind=kind[3], grid=True, ax=ax[1][1], fontsize=12 ) - if not create_features: - move_data.drop(columns=[DATE, HOUR, PERIOD, DAY], inplace=True) - if save_fig: plt.savefig(fname=name) + to_drop = list(set(move_data.columns) - set(columns)) + move_data.drop(columns=to_drop, inplace=True) + if return_fig: return fig @@ -151,6 +159,18 @@ def plot_trajectories( ------- figure The generated picture or None + + Examples + -------- + >>> from pymove.visualization.matplotlib import plot_trajectories + >>> move_df.head() + lat lon datetime id + 0 39.984094 116.319236 2008-10-23 05:53:05 1 + 1 39.984198 116.319322 2008-10-23 05:53:06 1 + 2 39.984224 116.319402 2008-10-23 05:53:11 1 + 3 39.984211 116.319389 2008-10-23 05:53:16 2 + 4 39.984217 116.319422 2008-10-23 05:53:21 2 + >>> plot_trajectories(move_df) """ fig = plt.figure(figsize=figsize) @@ -225,6 +245,18 @@ def plot_traj_by_id( IndexError If there is no trajectory with the tid passed + Examples + -------- + >>> from pymove.visualization.matplotlib import plot_traj_by_id + >>> move_df + lat lon datetime id + 0 39.984094 116.319236 2008-10-23 05:53:05 1 + 1 39.984198 116.319322 2008-10-23 05:53:06 1 + 2 39.984224 116.319402 2008-10-23 05:53:11 1 + 3 39.984211 116.319389 2008-10-23 05:53:16 2 + 4 39.984217 116.319422 2008-10-23 05:53:21 2 + >>> plot_traj_by_id(move_df_3, 1, label='id) + >>> plot_traj_by_id(move_df_3, 2, label='id) """ if label not in move_data: raise KeyError('%s feature not in dataframe' % label) @@ -256,10 +288,10 @@ def plot_traj_by_id( plt.plot( df_.iloc[0][LONGITUDE], df_.iloc[0][LATITUDE], 'yo', markersize=markersize - ) # start point + ) plt.plot( df_.iloc[-1][LONGITUDE], df_.iloc[-1][LATITUDE], 'yX', markersize=markersize - ) # end point + ) if save_fig: if not name: @@ -306,6 +338,17 @@ def plot_all_features( AttributeError If there are no columns with the specified type + Examples + -------- + >>> from pymove.visualization.matplotlib import plot_all_features + >>> move_df.head() + lat lon datetime id + 0 39.984094 116.319236 2008-10-23 05:53:05 1 + 1 39.984198 116.319322 2008-10-23 05:53:06 1 + 2 39.984224 116.319402 2008-10-23 05:53:11 1 + 3 39.984211 116.319389 2008-10-23 05:53:16 2 + 4 39.984217 116.319422 2008-10-23 05:53:21 2 + >>> plot_all_features(move_df) """ col_dtype = move_data.select_dtypes(include=[dtype]).columns tam = col_dtype.size @@ -341,6 +384,11 @@ def plot_coords(ax: axes, ob: BaseGeometry, color: Text = 'r'): Example ------- + >>> from pymove.visualization.matplotlib import plot_coords + >>> import matplotlib.pyplot as plt + >>> coords = LineString([(1, 1), (1, 2), (2, 2), (2, 3)]) + >>> _, ax = plt.subplots(figsize=(21, 9)) + >>> plot_coords(ax, coords) """ x, y = ob.xy ax.plot(x, y, 'o', color=color, zorder=1) @@ -348,7 +396,7 @@ def plot_coords(ax: axes, ob: BaseGeometry, color: Text = 'r'): def plot_bounds(ax: axes, ob: Union[LineString, MultiLineString], color='b'): """ - Plot the limites of geometric object. + Plot the limits of geometric object. Parameters ---------- @@ -361,7 +409,11 @@ def plot_bounds(ax: axes, ob: Union[LineString, MultiLineString], color='b'): Example ------- - + >>> from pymove.visualization.matplotlib import plot_bounds + >>> import matplotlib.pyplot as plt + >>> bounds = LineString([(1, 1), (1, 2), (2, 2), (2, 3)]) + >>> _, ax = plt.subplots(figsize=(21, 9)) + >>> plot_bounds(ax, bounds) """ x, y = zip(*list((p.x, p.y) for p in ob.boundary)) ax.plot(x, y, '-', color=color, zorder=1) @@ -398,6 +450,11 @@ def plot_line( Example ------- + >>> from pymove.visualization.matplotlib import plot_line + >>> import matplotlib.pyplot as plt + >>> line = LineString([(1, 1), (1, 2), (2, 2), (2, 3)]) + >>> _, ax = plt.subplots(figsize=(21, 9)) + >>> plot_line(ax, line) """ x, y = ob.xy ax.plot(