Skip to content

Commit

Permalink
Add plotting prelude
Browse files Browse the repository at this point in the history
  • Loading branch information
breimanntools committed Sep 26, 2023
1 parent 1a20194 commit 351d7c0
Show file tree
Hide file tree
Showing 55 changed files with 467 additions and 50 deletions.
Binary file modified aaanalysis/plotting/__pycache__/plot_gcfs_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/plotting/__pycache__/plot_get_cdict_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/plotting/__pycache__/plot_get_clist_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/plotting/__pycache__/plot_get_cmap_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/plotting/__pycache__/plot_set_legend_.cpython-39.pyc
Binary file not shown.
Binary file modified aaanalysis/plotting/__pycache__/plot_settings_.cpython-39.pyc
Binary file not shown.
3 changes: 3 additions & 0 deletions aaanalysis/plotting/plot_gcfs_.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ def plot_gcfs():
>>> plt.tight_layout()
>>> plt.show()
See Also
--------
* Our `Plotting Prelude <plotting_prelude.html>`_.
"""
# Get the current plotting context
current_context = sns.plotting_context()
Expand Down
3 changes: 3 additions & 0 deletions aaanalysis/plotting/plot_get_cdict_.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def plot_get_cdict(name: str = "DICT_COLOR") -> dict:
>>> plt.tight_layout()
>>> plt.show()
See Also
--------
* Our `Plotting Prelude <plotting_prelude.html>`_.
"""
list_names = [ut.STR_DICT_COLOR, ut.STR_DICT_CAT]
if name not in list_names:
Expand Down
2 changes: 1 addition & 1 deletion aaanalysis/plotting/plot_get_clist_.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def plot_get_clist(n_colors: int = 3) -> List[str]:
See Also
--------
* Example notebooks in `Plotting Prelude <plotting_prelude.html>`_.
* The example notebooks in `Plotting Prelude <plotting_prelude.html>`_.
* `Matplotlib color names <https://matplotlib.org/stable/gallery/color/named_colors.html>`_
* :func:`seaborn.color_palette` function to generate a color palette in seaborn.
"""
Expand Down
2 changes: 1 addition & 1 deletion aaanalysis/plotting/plot_get_cmap_.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def plot_get_cmap(name: str = "CPP",
See Also
--------
* Example notebooks in `Plotting Prelude <plotting_prelude.html>`_.
* Our `Plotting Prelude <plotting_prelude.html>`_.
* `Matplotlib color names <https://matplotlib.org/stable/gallery/color/named_colors.html>`_
* :func:`seaborn.color_palette` function to generate a color palette in seaborn.
* :func:`seaborn.light_palette function` to generate a lighter color palettes.
Expand Down
3 changes: 2 additions & 1 deletion aaanalysis/plotting/plot_set_legend_.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def plot_set_legend(ax: Optional[plt.Axes] = None,
marker: Optional[Union[str, int, list]] = None,
marker_size: Union[int, float, List[Union[int, float]]] = 10,
marker_linestyle: Union[str, list] = "-",
hatch: Optional[str] = None,
hatch: Optional[Union[str, List[str]]] = None,
# Title
title: str = None,
title_align_left: bool = True,
Expand Down Expand Up @@ -284,6 +284,7 @@ def plot_set_legend(ax: Optional[plt.Axes] = None,
See Also
--------
* More examples in `Plotting Prelude <plotting_prelude.html>`_.
* `Matplotlib markers <https://matplotlib.org/stable/api/markers_api.html>`.
* `Linestyles of markers <https://matplotlib.org/stable/gallery/lines_bars_and_markers/linestyles.html>`
* `Hatches <https://matplotlib.org/stable/gallery/shapes_and_collections/hatch_style_reference.html>`_ filling patterns.
Expand Down
28 changes: 8 additions & 20 deletions aaanalysis/plotting/plot_settings_.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,6 @@ def check_font(font="Arial"):
raise ValueError(error_message)


def check_fig_format(fig_format="pdf"):
""""""
list_fig_formats = ['eps', 'jpg', 'jpeg', 'pdf', 'pgf', 'png', 'ps',
'raw', 'rgba', 'svg', 'svgz', 'tif', 'tiff', 'webp']
ut.check_str(name="fig_format", val=fig_format)
if fig_format not in list_fig_formats:
raise ValueError(f"'fig_format' should be one of following: {list_fig_formats}")


def check_grid_axis(grid_axis="y"):
list_grid_axis = ["y", "x", "both"]
if grid_axis not in list_grid_axis:
Expand All @@ -50,7 +41,6 @@ def set_tick_size(axis=None, major_size=None, minor_size=None):
# II Main functions
def plot_settings(font_scale: float = 1,
font: str = "Arial",
fig_format: str = "pdf",
weight_bold: bool = True,
adjust_only_font: bool = False,
adjust_further_elements: bool = True,
Expand All @@ -67,16 +57,15 @@ def plot_settings(font_scale: float = 1,
Configures general settings for plot visualization.
This function modifies the global settings of :mod:`matplotlib` and :mod:`seaborn` libraries.
PDFs are embedded such that they can be edited using image editing software.
It adjusts font embedding for vector formats like PDF and SVG, ensuring compatibility and
editability across various viewers and editing software.
Parameters
----------
font_scale
Scaling factor to scale the size of font elements. Consistent with :func:`seaborn.set_context`.
font
Name of text font. Common options are 'Arial', 'Verdana', 'Helvetica', or 'DejaVu Sans' (Matplotlib default).
fig_format
Specifies the file format for saving plots. Most backends support png, pdf, ps, eps and svg.
weight_bold
If ``True``, font and line elements are bold.
adjust_only_font
Expand Down Expand Up @@ -138,13 +127,13 @@ def plot_settings(font_scale: float = 1,
See Also
--------
* More examples in `Plotting Prelude <plotting_prelude.html>`_.
* :func:`seaborn.set_context`, where ``font_scale`` is utilized.
* :data:`matplotlib.rcParams`, which manages the global settings in :mod:`matplotlib`.
"""
# Check input
ut.check_number_range(name="font_scale", val=font_scale, min_val=0, just_int=False)
check_font(font=font)
check_fig_format(fig_format=fig_format)
check_grid_axis(grid_axis=grid_axis)
args_bool = {"weight_bold": weight_bold, "adjust_only_font": adjust_only_font,
"adjust_further_elements": adjust_further_elements, "grid": grid,
Expand Down Expand Up @@ -186,6 +175,7 @@ def plot_settings(font_scale: float = 1,
# Grid
plt.rcParams["axes.grid.axis"] = grid_axis
plt.rcParams["axes.grid"] = grid
plt.rcParams["grid.linewidth"] = 1 if weight_bold else 0.8
# Adjust weight of text and lines
if weight_bold:
plt.rcParams["axes.labelweight"] = "bold"
Expand All @@ -204,7 +194,6 @@ def plot_settings(font_scale: float = 1,
short_minor_size = 2
no_major_size = 0
no_minor_size = 0

# Set x ticks
if no_ticks or no_ticks_x:
set_tick_size(axis="x", major_size=no_major_size, minor_size=no_minor_size)
Expand All @@ -221,11 +210,10 @@ def plot_settings(font_scale: float = 1,
else:
set_tick_size(axis="y", major_size=default_major_size, minor_size=default_minor_size)

# Handle figure format
if fig_format == "pdf":
mpl.rcParams['pdf.fonttype'] = 42
elif "svg" in fig_format:
mpl.rcParams['svg.fonttype'] = 'none'
# Handle storing of vectorized figure format
mpl.rcParams['pdf.fonttype'] = 42 # TrueType
mpl.rcParams['ps.fonttype'] = 42
mpl.rcParams['svg.fonttype'] = 'none'

# Additional adjustments
if adjust_further_elements:
Expand Down
Binary file modified docs/build/doctrees/environment.pickle
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_gcfs.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_get_cdict.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_get_clist.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_get_cmap.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_set_legend.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/aaanalysis.plot_settings.doctree
Binary file not shown.
Binary file modified docs/build/doctrees/generated/plotting_prelude.doctree
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added docs/build/html/_images/output_3_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/build/html/_images/output_5_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/build/html/_images/output_7_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/build/html/_images/output_9_0.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
90 changes: 87 additions & 3 deletions docs/build/html/_sources/generated/plotting_prelude.rst.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,91 @@
Plotting prelude
================

This is a short overview of our utility plotting functions to make
publication-ready visualizations with a view extra lines of code using
the three following functions:
These are some of our utility plotting functions to make
publication-ready visualizations with a view extra lines of code.

Let us first make all imports and create some data

.. code:: ipython2
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
data = {'Classes': ['Class A', 'Class B', 'Class C'], 'Values': [23, 27, 43]}
The default seaborn output with removed top and right spines looks like
this:

.. code:: ipython2
sns.barplot(x='Classes', y='Values', data=data)
sns.despine()
plt.title("Seaborn default")
plt.tight_layout()
plt.show()
.. image:: output_3_0.png


Just call our ``aa.plot_setting`` function with our optimized color set
to get this:

.. code:: ipython2
import aaanalysis as aa
colors = aa.plot_get_clist()
aa.plot_settings()
sns.barplot(x='Classes', y='Values', data=data, palette=colors)
sns.despine()
plt.title("Adjusted by AAanalysis 1")
plt.tight_layout()
plt.show()
.. image:: output_5_0.png


The settings can be easily adjusted and colors are provided for up to 9
classes:

.. code:: ipython2
data = {'Classes': ['Class A', 'Class B', 'Class C', "Class D", "Class E"], 'Values': [23, 27, 43, 9, 14]}
colors = aa.plot_get_clist(n_colors=5)
aa.plot_settings(no_ticks_x=True, short_ticks_y=True, grid=True, grid_axis="y")
sns.barplot(x='Classes', y='Values', data=data, palette=colors)
sns.despine()
plt.title("Adjusted by AAanalysis 2")
plt.tight_layout()
plt.show()
.. image:: output_7_0.png


Retrieve the set font size and create an independent legend like this:

.. code:: ipython2
data = {'Classes': ['Class A', 'Class B', 'Class C', "Class D", "Class E"], 'Values': [23, 27, 43, 9, 14]}
colors = aa.plot_get_clist(n_colors=5)
aa.plot_settings(no_ticks_x=True, short_ticks_y=True)
sns.barplot(x='Classes', y='Values', data=data, palette=colors, hatch=["/", "/", "/", ".", "."])
fontsize = aa.plot_gcfs()
sns.despine()
plt.title("Adjusted by AAanalysis 3")
dict_color = {"Group 1": "black", "Group 2": "black"}
aa.plot_set_legend(dict_color=dict_color, ncol=1, x=0.7, y=0.9, hatch=["/", "."])
plt.tight_layout()
plt.show()
.. image:: output_9_0.png

8 changes: 7 additions & 1 deletion docs/build/html/generated/aaanalysis.plot_gcfs.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<h1>aaanalysis.plot_gcfs<a class="headerlink" href="#aaanalysis-plot-gcfs" title="Permalink to this heading"></a></h1>
<dl class="py function">
<dt class="sig sig-object py" id="aaanalysis.plot_gcfs">
<span class="sig-prename descclassname"><span class="pre">aaanalysis.</span></span><span class="sig-name descname"><span class="pre">plot_gcfs</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/breimanntools/aaanalysis/tree/master/aaanalysis/plotting/plot_gcfs_.py#L7-L36"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#aaanalysis.plot_gcfs" title="Permalink to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">aaanalysis.</span></span><span class="sig-name descname"><span class="pre">plot_gcfs</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="reference external" href="https://github.com/breimanntools/aaanalysis/tree/master/aaanalysis/plotting/plot_gcfs_.py#L7-L39"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#aaanalysis.plot_gcfs" title="Permalink to this definition"></a></dt>
<dd><p>Gets current font size.</p>
<p>This font size can be set by <a class="reference internal" href="aaanalysis.plot_settings.html#aaanalysis.plot_settings" title="aaanalysis.plot_settings"><code class="xref py py-func docutils literal notranslate"><span class="pre">plot_settings()</span></code></a> function.</p>
<div class="admonition-examples admonition">
Expand All @@ -154,6 +154,12 @@ <h1>aaanalysis.plot_gcfs<a class="headerlink" href="#aaanalysis-plot-gcfs" title
<img alt="../_images/aaanalysis-plot_gcfs-1.png" class="plot-directive" src="../_images/aaanalysis-plot_gcfs-1.png" />
</figure>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<ul class="simple">
<li><p>Our <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
</ul>
</div>
</dd></dl>

</section>
Expand Down
8 changes: 7 additions & 1 deletion docs/build/html/generated/aaanalysis.plot_get_cdict.html
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
<h1>aaanalysis.plot_get_cdict<a class="headerlink" href="#aaanalysis-plot-get-cdict" title="Permalink to this heading"></a></h1>
<dl class="py function">
<dt class="sig sig-object py" id="aaanalysis.plot_get_cdict">
<span class="sig-prename descclassname"><span class="pre">aaanalysis.</span></span><span class="sig-name descname"><span class="pre">plot_get_cdict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'DICT_COLOR'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/breimanntools/aaanalysis/tree/master/aaanalysis/plotting/plot_get_cdict_.py#L5-L46"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#aaanalysis.plot_get_cdict" title="Permalink to this definition"></a></dt>
<span class="sig-prename descclassname"><span class="pre">aaanalysis.</span></span><span class="sig-name descname"><span class="pre">plot_get_cdict</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">name</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'DICT_COLOR'</span></span></em><span class="sig-paren">)</span><a class="reference external" href="https://github.com/breimanntools/aaanalysis/tree/master/aaanalysis/plotting/plot_get_cdict_.py#L5-L49"><span class="viewcode-link"><span class="pre">[source]</span></span></a><a class="headerlink" href="#aaanalysis.plot_get_cdict" title="Permalink to this definition"></a></dt>
<dd><p>Returns color dictionarie specified for AAanalysis.</p>
<dl class="field-list simple">
<dt class="field-odd">Parameters<span class="colon">:</span></dt>
Expand Down Expand Up @@ -170,6 +170,12 @@ <h1>aaanalysis.plot_get_cdict<a class="headerlink" href="#aaanalysis-plot-get-cd
<img alt="../_images/aaanalysis-plot_get_cdict-1.png" class="plot-directive" src="../_images/aaanalysis-plot_get_cdict-1.png" />
</figure>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<ul class="simple">
<li><p>Our <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
</ul>
</div>
</dd></dl>

</section>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/generated/aaanalysis.plot_get_clist.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ <h1>aaanalysis.plot_get_clist<a class="headerlink" href="#aaanalysis-plot-get-cl
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<ul class="simple">
<li><p>Example notebooks in <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
<li><p>The example notebooks in <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
<li><p><a class="reference external" href="https://matplotlib.org/stable/gallery/color/named_colors.html">Matplotlib color names</a></p></li>
<li><p><a class="reference external" href="https://seaborn.pydata.org/generated/seaborn.color_palette.html#seaborn.color_palette" title="(in seaborn v0.12.2)"><code class="xref py py-func docutils literal notranslate"><span class="pre">seaborn.color_palette()</span></code></a> function to generate a color palette in seaborn.</p></li>
</ul>
Expand Down
2 changes: 1 addition & 1 deletion docs/build/html/generated/aaanalysis.plot_get_cmap.html
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ <h1>aaanalysis.plot_get_cmap<a class="headerlink" href="#aaanalysis-plot-get-cma
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<ul class="simple">
<li><p>Example notebooks in <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
<li><p>Our <a class="reference external" href="plotting_prelude.html">Plotting Prelude</a>.</p></li>
<li><p><a class="reference external" href="https://matplotlib.org/stable/gallery/color/named_colors.html">Matplotlib color names</a></p></li>
<li><p><a class="reference external" href="https://seaborn.pydata.org/generated/seaborn.color_palette.html#seaborn.color_palette" title="(in seaborn v0.12.2)"><code class="xref py py-func docutils literal notranslate"><span class="pre">seaborn.color_palette()</span></code></a> function to generate a color palette in seaborn.</p></li>
<li><p><code class="xref py py-func docutils literal notranslate"><span class="pre">seaborn.light_palette</span> <span class="pre">function()</span></code> to generate a lighter color palettes.</p></li>
Expand Down
Loading

0 comments on commit 351d7c0

Please sign in to comment.