{ "cells": [ { "cell_type": "markdown", "id": "intro", "metadata": {}, "source": [ "# `%f` — fractional seconds (milliseconds)\n", "\n", "The new `%f` pattern formats the millisecond component of a datetime, zero-padded to 3 digits (e.g. `007`, `123`)." ] }, { "cell_type": "code", "execution_count": 1, "id": "imports", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", " \n", " \n", " " ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import pandas as pd\n", "from lets_plot import *\n", "\n", "LetsPlot.setup_html()" ] }, { "cell_type": "code", "execution_count": 2, "id": "data", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
tvalue
02025-06-01 12:00:00.0000
12025-06-01 12:00:00.1171
22025-06-01 12:00:00.2342
32025-06-01 12:00:00.3513
42025-06-01 12:00:00.4684
52025-06-01 12:00:00.5855
62025-06-01 12:00:00.7026
72025-06-01 12:00:00.8197
82025-06-01 12:00:00.9368
92025-06-01 12:00:01.0539
102025-06-01 12:00:01.17010
112025-06-01 12:00:01.28711
\n", "
" ], "text/plain": [ " t value\n", "0 2025-06-01 12:00:00.000 0\n", "1 2025-06-01 12:00:00.117 1\n", "2 2025-06-01 12:00:00.234 2\n", "3 2025-06-01 12:00:00.351 3\n", "4 2025-06-01 12:00:00.468 4\n", "5 2025-06-01 12:00:00.585 5\n", "6 2025-06-01 12:00:00.702 6\n", "7 2025-06-01 12:00:00.819 7\n", "8 2025-06-01 12:00:00.936 8\n", "9 2025-06-01 12:00:01.053 9\n", "10 2025-06-01 12:00:01.170 10\n", "11 2025-06-01 12:00:01.287 11" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = pd.DataFrame({\n", " 't': pd.date_range('2025-06-01 12:00:00', periods=12, freq='117ms'),\n", "})\n", "df['value'] = range(len(df))\n", "df" ] }, { "cell_type": "markdown", "id": "md-axis", "metadata": {}, "source": [ "## On the axis" ] }, { "cell_type": "code", "execution_count": 3, "id": "c-ms-only", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Milliseconds only\n", "ggplot(df, aes('t', 'value')) + geom_point() + ggsize(700, 250) \\\n", " + scale_x_datetime(format='%f')" ] }, { "cell_type": "code", "execution_count": 4, "id": "c-sec-ms", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Seconds + milliseconds\n", "ggplot(df, aes('t', 'value')) + geom_point() + ggsize(700, 250) \\\n", " + scale_x_datetime(format='%S.%f')" ] }, { "cell_type": "code", "execution_count": 5, "id": "c-iso", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Full ISO-like timestamp with milliseconds\n", "ggplot(df, aes('t', 'value')) + geom_point() + ggsize(700, 250) \\\n", " + scale_x_datetime(format='%Y-%m-%dT%H:%M:%S.%f')" ] }, { "cell_type": "markdown", "id": "md-tooltip", "metadata": {}, "source": [ "## In tooltips" ] }, { "cell_type": "code", "execution_count": 6, "id": "c-tooltip", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "
\n", "
\n", "
\n", " \n", "
\n", " \n", "
\n", "
\n", " \n", " \n", " \n", " " ], "text/plain": [ "" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ggplot(df, aes('t', 'value')) + ggsize(700, 250) \\\n", " + geom_point(tooltips=layer_tooltips()\n", " .format('^x', '%H:%M:%S.%f')\n", " .line('t|^x')\n", " .line('value|^y')) \\\n", " + scale_x_datetime(format='%S.%f')" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.20" } }, "nbformat": 4, "nbformat_minor": 5 }