teilab.plot.plotly module¶
A group of plot functions useful for analysis using plotly.
If you would like to
make changes to the plot or draw other plots, please refer to the official documentation.
see the analysis examples, please refer to the notebook/Local/Main-Lecture-Material-plotly.ipynb
※ Compared to teilab.plot.matplotlib module, you can see the difference between the two libraries. (matplotlib, and plotly)
- teilab.plot.plotly.density_plot(data: nptyping.types._ndarray.NDArray[Any, Any, nptyping.types._object.Object], labels: List[str] = [], colors: List[Any] = [], cmap: Optional[Union[str, matplotlib.colors.Colormap]] = None, bins: int = 100, range: Optional[Tuple[float, float]] = None, title: str = 'Density Distribution', fig: Optional[plotly.graph_objs._figure.Figure] = None, row: int = 1, col: int = 1, plotkwargs: Dict[str, Any] = {}, layoutkwargs: Dict[str, Any] = {}, **kwargs) plotly.graph_objs._figure.Figure [source]¶
Plot density dirstibutions.
- Parameters
data (NDArray[(Any,Any),Number]) – Input data. Shape = (
n_samples
,n_features
)labels (List[str], optional) – Labels for each sample. Defaults to
[]
.colors (List[Any], optional) – Colors for each sample. Defaults to
[]
.cmap (Optional[Union[str,Colormap]], optional) – A
Colormap
object or a color map name. Defaults toNone
.bins (int, optional) – The number of equal-width bins in the given range. Defaults to
100
.range (Optional[Tuple[float,float]], optional) – The lower and upper range of the bins. If not provided, range is simply
(data[i].min(), data[i].max())
. Defaults toNone
.title (str, optional) – Figure Title. Defaults to
"Density Distribution"
.fig (Optional[Figure], optional) – An instance of Figure.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.plotkwargs (Dict[str,Any]) – Keyword arguments for
go.Scatter
. Defaults to{}
.layoutkwargs (Dict[str,Any]) – Keyword arguments for
update_layout
. Defaults to{}
.
- Returns
An instance of
Figure
with density distributions.- Return type
Figure
>>> import numpy as np >>> from teilab.utils import dict2str, subplots_create >>> from teilab.plot.plotly import density_plot >>> n_samples, n_features = (4, 1000) >>> data = np.random.RandomState(0).normal(loc=np.expand_dims(np.arange(n_samples), axis=1), size=(n_samples, n_features)) >>> kwargses = [{"bins":100},{"bins":10},{"bins":"auto"}] >>> title = ", ".join([dict2str(kwargs) for kwargs in kwargses]) >>> nfigs = len(kwargses) >>> fig = subplots_create(ncols=nfigs, style="plotly") >>> for i,(kwargs) in enumerate(kwargses, start=1): ... _ = density_plot(data, fig=fig, title=title, col=i, legend=False, width=1000, height=400, **kwargs) >>> fig.show()
- teilab.plot.plotly.boxplot(data: nptyping.types._ndarray.NDArray[Any, Any, nptyping.types._object.Object], labels: List[str] = [], colors: List[Any] = [], cmap: Optional[Union[str, matplotlib.colors.Colormap]] = None, vert: bool = True, title: str = 'Box Plot', fig: Optional[plotly.graph_objs._figure.Figure] = None, row: int = 1, col: int = 1, plotkwargs: Dict[str, Any] = {}, layoutkwargs: Dict[str, Any] = {}, **kwargs) plotly.graph_objs._figure.Figure [source]¶
Plot box plots.
- Parameters
data (NDArray[(Any,Any),Number]) – Input data. Shape = (
n_samples
,n_features
)labels (List[str], optional) – Labels for each sample. Defaults to
[]
.colors (List[Any], optional) – Colors for each sample. Defaults to
[]
.cmap (Optional[Union[str,Colormap]], optional) – A
Colormap
object or a color map name. Defaults toNone
.vert (bool, optional) – Whether to draw vertical boxes or horizontal boxes. Defaults to
True
.title (str, optional) – Figure Title. Defaults to
"Box Plot"
.fig (Optional[Figure], optional) – An instance of Figure.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.plotkwargs (Dict[str,Any]) – Keyword arguments for
go.Box
. Defaults to{}
.layoutkwargs (Dict[str,Any]) – Keyword arguments for
update_layout
. Defaults to{}
.
- Returns
An instance of
Figure
with box plot- Return type
Figure
>>> import numpy as np >>> from teilab.utils import dict2str, subplots_create >>> from teilab.plot.plotly import boxplot >>> n_samples, n_features = (4, 1000) >>> data = np.random.RandomState(0).normal(loc=np.expand_dims(np.arange(n_samples), axis=1), size=(n_samples, n_features)) >>> kwargses = [{"vert":True},{"vert":False}] >>> title = ", ".join([dict2str(kwargs) for kwargs in kwargses]) >>> nfigs = len(kwargses) >>> fig = subplots_create(ncols=nfigs, style="plotly") >>> for col,kwargs in enumerate(kwargses, start=1): ... _ = boxplot(data, title=title, fig=fig, col=col, width=1000, height=400, **kwargs) >>> fig.show()
- teilab.plot.plotly.cumulative_density_plot(data: Union[nptyping.types._ndarray.NDArray[Any, Any, nptyping.types._object.Object], pandas.core.series.Series], labels: List[str] = [], colors: List[Any] = [], cmap: Optional[Union[str, matplotlib.colors.Colormap]] = None, title: str = 'Cumulative Density Distribution', ylabel='Frequency', fig: Optional[plotly.graph_objs._figure.Figure] = None, row: int = 1, col: int = 1, plotkwargs: Dict[str, Any] = {}, layoutkwargs: Dict[str, Any] = {}, **kwargs) plotly.graph_objs._figure.Figure [source]¶
Plot cumulative density dirstibutions.
- Parameters
data (Union[NDArray[(Any,Any),Number],Series]) – Input data. Shape = (
n_samples
,n_features
)labels (List[str], optional) – Labels for each sample. Defaults to
[]
.colors (List[Any], optional) – Colors for each sample. Defaults to
[]
.cmap (Optional[Union[str,Colormap]], optional) – A
Colormap
object or a color map name. Defaults toNone
.title (str, optional) – Figure Title. Defaults to
"Cumulative Density Distribution"
.ylabel (str, optional) – Figure y-axis label. Defaults to
"Frequency"
fig (Optional[Figure], optional) – An instance of Figure.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.plotkwargs (Dict[str,Any]) – Keyword arguments for
go.Scatter
. Defaults to{}
.layoutkwargs (Dict[str,Any]) – Keyword arguments for
update_layout
. Defaults to{}
.
- Returns
An instance of
Figure
with cumulative density distributions.- Return type
Figure
>>> import numpy as np >>> from teilab.plot.plotly import cumulative_density_plot >>> n_samples, n_features = (4, 1000) >>> data = np.random.RandomState(0).normal(loc=np.expand_dims(np.arange(n_samples), axis=1), size=(n_samples, n_features)) >>> fig = cumulative_density_plot(data, fig=None, xlabel="value", width=800, height=400) >>> fig.show()
- teilab.plot.plotly.XYplot(df: pandas.core.frame.DataFrame, x: str, y: str, logarithmic: bool = True, color: Optional[str] = None, symbol: Optional[str] = None, size: Optional[str] = None, hover_data: Optional[List[str]] = None, hover_name: Optional[str] = None, fig: Optional[plotly.graph_objs._figure.Figure] = None, row: int = 1, col: int = 1, plotkwargs: Dict[str, Any] = {}, layoutkwargs: Dict[str, Any] = {}, **kwargs) plotly.graph_objs._figure.Figure [source]¶
XY plot.
x-axis : \(\log_2{(\text{gProcessedSignal})}\) for each gene in sample
X
y-axis : \(\log_2{(\text{gProcessedSignal})}\) for each gene in sample
Y
- Parameters
df (pd.DataFrame) – DataFrame
x (str) – The column name for sample
X
.y (str) – The column name for sample
Y
.logarithmic (bool) – Whether to log the values of
df[x]
anddf[y]
color (Optional[str], optional) – The column name in
df
to assign color to marks. Defaults toNone
.symbol (Optional[str], optional) – The column name in
df
to assign symbols to marks. Defaults toNone
.size (Optional[str], optional) – The column name in
df
to assign mark sizes. Defaults toNone
.hover_data (Optional[List[str]], optional) – Values in this column appear in bold in the hover tooltip. Defaults to
None
.hover_name (Optional[str], optional) – Values in this column appear in the hover tooltip. Defaults to
None
.fig (Optional[Figure], optional) – An instance of Figure.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.plotkwargs (Dict[str,Any]) – Keyword arguments for
go.Scatter
. Defaults to{}
.layoutkwargs (Dict[str,Any]) – Keyword arguments for
update_layout
. Defaults to{}
.
- Returns
An instance of
Figure
with XY plot.- Return type
Figure
>>> import pandas as pd >>> from teilab.datasets import TeiLabDataSets >>> from teilab.plot.plotly import XYplot >>> datasets = TeiLabDataSets(verbose=False) >>> df_anno = datasets.read_data(no=0, usecols=datasets.ANNO_COLNAMES) >>> reliable_index = set(df_anno.index) >>> df_combined = df_anno.copy(deep=True) >>> for no in range(2): ... df_data = datasets.read_data(no=no) ... reliable_index = reliable_index & set(datasets.reliable_filter(df=df_data)) ... df_combined = pd.concat([ ... df_combined, ... df_data[[datasets.TARGET_COLNAME]].rename(columns={datasets.TARGET_COLNAME: datasets.samples.Condition[no]}) ... ], axis=1) >>> df_combined = df_combined.loc[reliable_index, :].reset_index(drop=True) >>> fig = XYplot(df=df_combined, x=datasets.samples.Condition[0], y=datasets.samples.Condition[1], hover_name="SystematicName", height=600, width=600) >>> fig.show()
- teilab.plot.plotly.MAplot(df: pandas.core.frame.DataFrame, x: str, y: str, color: Optional[str] = None, symbol: Optional[str] = None, size: Optional[str] = None, hover_data: Optional[List[str]] = None, hover_name: Optional[str] = None, hlines: Union[Dict[numbers.Number, Dict[str, Any]], List[numbers.Number]] = [], fig: Optional[plotly.graph_objs._figure.Figure] = None, row: int = 1, col: int = 1, plotkwargs: Dict[str, Any] = {}, layoutkwargs: Dict[str, Any] = {}, **kwargs) plotly.graph_objs._figure.Figure [source]¶
MA plot.
x-axis (Average) : \(\log_{10}{\left(\text{gProcessedSignal}_X\times\text{gProcessedSignal}_Y\right)}\)
y-axis (Minus) : \(\log_{2}{\left(\text{gProcessedSignal}_Y / \text{gProcessedSignal}_X\right)}\)
- Parameters
df (pd.DataFrame) – DataFrame
x (str) – The column name for sample
X
.y (str) – The column name for sample
Y
.color (Optional[str], optional) – The column name in
df
to assign color to marks. Defaults toNone
.symbol (Optional[str], optional) – The column name in
df
to assign symbols to marks. Defaults toNone
.size (Optional[str], optional) – The column name in
df
to assign mark sizes. Defaults toNone
.hover_data (Optional[List[str]], optional) – Values in this column appear in bold in the hover tooltip. Defaults to
None
.hover_name (Optional[str], optional) – Values in this column appear in the hover tooltip. Defaults to
None
.hlines (Union[Dict[Number,Dict[str,Any]],List[Number]]) – Height (
y
) to draw a horizon. If given a dictionary, values means kwargs ofax.hlines
fig (Optional[Figure], optional) – An instance of Figure.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.plotkwargs (Dict[str,Any]) – Keyword arguments for
go.Scatter
. Defaults to{}
.layoutkwargs (Dict[str,Any]) – Keyword arguments for
update_layout
. Defaults to{}
.
- Returns
An instance of
Figure
with MA plot.- Return type
Figure
>>> import pandas as pd >>> from teilab.datasets import TeiLabDataSets >>> from teilab.plot.plotly import MAplot >>> datasets = TeiLabDataSets(verbose=False) >>> df_anno = datasets.read_data(no=0, usecols=datasets.ANNO_COLNAMES) >>> reliable_index = set(df_anno.index) >>> df_combined = df_anno.copy(deep=True) >>> for no in range(2): ... df_data = datasets.read_data(no=no) ... reliable_index = reliable_index & set(datasets.reliable_filter(df=df_data)) ... df_combined = pd.concat([ ... df_combined, ... df_data[[datasets.TARGET_COLNAME]].rename(columns={datasets.TARGET_COLNAME: datasets.samples.Condition[no]}) ... ], axis=1) >>> df_combined = df_combined.loc[reliable_index, :].reset_index(drop=True) >>> fig = MAplot( ... df=df_combined, ... x=datasets.samples.Condition[0], y=datasets.samples.Condition[1], hover_name="SystematicName", ... hlines={ ... -1 : dict(fillcolor="red", marker={"color":"red"}, line={"width":1}, showlegend=False), ... 0 : dict(fillcolor="red", marker={"color":"red"}, line={"width":3}, showlegend=False), ... 1 : dict(fillcolor="red", marker={"color":"red"}, line={"width":1}, showlegend=False), ... }, ... height=600, width=600, >>> ) >>> fig.show()
- teilab.plot.plotly.update_layout(fig: plotly.graph_objs._figure.Figure, row: int = 1, col: int = 1, title: Optional[str] = None, xlabel: Optional[str] = None, ylabel: Optional[str] = None, xlim: Optional[Tuple[Any, Any]] = None, ylim: Optional[Tuple[Any, Any]] = None, xrangeslider: Dict[str, Any] = {'visible': False}, font: Dict[str, Any] = {'family': 'verdana, arial, sans-serif', 'size': 14}, legend: bool = True, xaxis_type: str = 'linear', yaxis_type: str = 'linear', legend_tracegroupgap: int = 100, width: int = 600, height: int = 600, template: str = 'presentation') plotly.graph_objs._figure.Figure [source]¶
Update the layout of
plotly.graph_objs._figure.Figure
object. See Documentation for details.- Parameters
fig (Figure) – A Figure object.
row (int, optional) – Row of subplots. Defaults to
1
.col (int, optional) – Column of subplots. Defaults to
1
.title (Optional[str], optional) – Figure title. Defaults to
None
.xlabel (Optional[str], optional) – X axis label. Defaults to
None
.ylabel (Optional[str], optional) – Y axis label. Defaults to
None
.xlim (Optional[Tuple[Any,Any]], optional) – X axis range. Defaults to
None
.ylim (Optional[Tuple[Any,Any]], optional) – Y axis range. Defaults to
None
.xrangeslider (Dict[str,Any], optional) – Keyword arguments for Range Slider. Defaults to
{"visible": False}
.font (Dict[str,Any], optional) – Keyword arguments for Fonts. Defaults to
{"family": "Meiryo", "size": 20}
.legend (bool, optional) – Whether to show legend. Defaults to
True
.xaxis_type (str, optional) – X axis type. Defaults to
"linear"
.yaxis_type (str, optional) – Y axis type. Defaults to
"linear"
.legend_tracegroupgap (int, optional) – Gap between legend groups. Defaults to
100
.width (int, optional) – Figure width. Defaults to
600
.height (int, optional) – Figure height. Defaults to
600
.template (str, optional) – Plotly themes. You can check the all template by
python -c "import plotly.io as pio; print(pio.templates)
. Defaults to"plotly_white"
.
- Returns
Figure object with layout updated.
- Return type
Figure
>>> import plotly.graph_objects as go >>> from teilab.utils import subplots_create >>> from teilab.plot.plotly import update_layout >>> fig = subplots_create(nrows=1, ncols=2, style="plotly") >>> for c in range(1,3): ... fig.add_trace(go.Scatter(x=[1,2,3],y=[4,5,6]),row=1,col=c) >>> fig = update_layout(fig=fig, title="Sample", ylim=(4.5,5.5), col=2, height=400) >>> fig.show()