Skip to content
Snippets Groups Projects

Add live_view module to qutil.plotting.

Merged Tobias Hangleiter requested to merge feat/live_data_view into master
Compare and
2 files
+ 662
2
Compare changes
  • Side-by-side
  • Inline
Files
2
+ 57
2
@@ -11,15 +11,23 @@ import dataclasses
import pathlib
import warnings
from contextlib import contextmanager
from typing import Sequence, Tuple, Mapping, Any, Union, List, Callable
from typing import Sequence, Tuple, Mapping, Any, Union, List, Callable, Literal
from weakref import WeakValueDictionary
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import numpy.typing as npt
import pandas as pd
from cycler import cycler
from matplotlib import pyplot as plt
from matplotlib import ticker
from ..typecheck import check_literals
try:
import qcodes
except ImportError:
qcodes = None
__all__ = [
"CoordClicker",
@@ -33,6 +41,7 @@ __all__ = [
"cycle_plots",
"update_plot",
"list_styles",
"reformat_axis",
"get_rwth_color_cycle",
"rwth_color_cycle",
"rwth_color_cycle_25",
@@ -707,6 +716,52 @@ def list_styles(include_matplotlib_styles: bool = False) -> List[str]:
return custom_styles
@check_literals
def reformat_axis(ax_or_cbar: mpl.axes.Axes | mpl.colorbar.Colorbar, data: npt.ArrayLike,
unit: str, which: Literal['x', 'y', 'c']) -> str:
"""Scales an axis with SI prefixes.
.. note::
This function requires :mod:`qcodes` to be installed.
Parameters
----------
ax_or_cbar :
The matplotlib :class:`~matplotlib:matplotlib.axes.Axes` or
:class:`~matplotlib:matplotlib.colorbar.Colorbar` to reformat.
data :
The data to reformat.
unit :
The unit of the data.
which :
The type of axis. Either x, y, or c for colorbar axis.
Returns
-------
prefix :
The SI unit prefix for the reformatted data.
"""
if qcodes is None:
raise RuntimeError('This function requires qcodes.')
if np.isnan(data).all():
# preempt unhandled exception in find_scale_and_prefix
return ''
prefix, scale = qcodes.plotting.axis_labels.find_scale_and_prefix(data, unit)
formatter = ticker.FuncFormatter(lambda value, pos: f"{value * 10 ** (-scale):g}")
if which == 'x':
ax_or_cbar.xaxis.set_major_formatter(formatter)
elif which == 'y':
ax_or_cbar.yaxis.set_major_formatter(formatter)
else:
ax_or_cbar.formatter = formatter
ax_or_cbar.update_ticks()
return prefix
@dataclasses.dataclass
class LaTeXPlotHelper:
A4_SIZE_INCHES = (8.25, 11.75)
Loading