From e11147f071fc2dc7d02cc049b5749f9888aa38d6 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Mon, 29 Mar 2021 15:02:37 +0200 Subject: [PATCH 1/8] implemented Range.make_plot() --- doc/source/changes/version_0_33.rst.inc | 3 ++ larray/inout/xw_excel.py | 64 +++++++++++++++++++++++++ larray/tests/test_excel.py | 16 +++++++ 3 files changed, 83 insertions(+) diff --git a/doc/source/changes/version_0_33.rst.inc b/doc/source/changes/version_0_33.rst.inc index 1f3a3b71b..b494d7434 100644 --- a/doc/source/changes/version_0_33.rst.inc +++ b/doc/source/changes/version_0_33.rst.inc @@ -65,6 +65,9 @@ Miscellaneous improvements * implemented :py:obj:`Axis.min()` and :py:obj:`Axis.max()` methods (closes :issue:`874`). +* implemented the :py:obj:`Range.make_plot()` method that allows to create graphs when using + `open_excel()` (closes :issue:`900`). + Fixes ^^^^^ diff --git a/larray/inout/xw_excel.py b/larray/inout/xw_excel.py index 6ba5e621c..c6d92ecc9 100644 --- a/larray/inout/xw_excel.py +++ b/larray/inout/xw_excel.py @@ -1,6 +1,8 @@ import os import atexit +from typing import Union, Dict, Callable, Any + import numpy as np try: import xlwings as xw @@ -618,6 +620,67 @@ def load(self, header=True, convert_float=True, nb_axes=None, index_col=None, fi else: return Array(list_data) + def make_plot(self, position: str, width: int=427, height: int=230, title: str=None, template: str=None, + min_y: Union[int, float]=None, max_y: Union[int, float]=None, + xticks_spacing: Union[int, float]=None, customize_func: Callable=None, + customize_kwargs: Dict[str, str]=None) -> Any: + from xlwings.constants import LegendPosition, ChartType, RowCol, AxisType, Constants, Direction + if customize_func is not None and not callable(customize_func): + raise TypeError(f"Expected a function for the argument 'customize_func'. " + f"Got object of type {type(customize_func).__name__} instead.") + if template is not None and not os.path.isfile(template): + raise ValueError(f"Could not find template file {template}") + title = str(title) if title is not None else None + sheet = self.sheet.xw_sheet.api + data_range = self.xw_range.api + top_left_cell = data_range.Cells(1, 1) + # expand if current range is one cell + if data_range.Count == 1: + bottom_left_cell = data_range.End(Direction.xlDown) + bottom_right_cell = bottom_left_cell.End(Direction.xlToRight) + data_range = sheet.Range(top_left_cell, bottom_right_cell) + # horrible hack to make sure that Excel will consider the first column as the xticks + top_left_cell_value = top_left_cell.Value + top_left_cell.Value = '' + # start chart + sheet_charts = sheet.ChartObjects() + position = sheet.Range(position) + left, top = position.Left, position.Top + obj = sheet_charts.Add(left, top, width, height) + obj_chart = obj.Chart + source = data_range + obj_chart.SetSourceData(source) + obj_chart.ChartType = ChartType.xlLine + # title + if title is not None: + obj_chart.HasTitle = True + obj_chart.ChartTitle.Caption = title + # legend + obj_chart.Legend.Position = LegendPosition.xlLegendPositionBottom + # template + if template is not None: + obj_chart.ApplyChartTemplate(template) + # min - max on Y axis + if min_y is not None: + obj_chart.Axes(AxisType.xlValue).MinimumScale = min_y + if max_y is not None: + obj_chart.Axes(AxisType.xlValue).MaximumScale = max_y + # xticks_spacing + if xticks_spacing is not None: + obj_chart.Axes(AxisType.xlCategory).TickLabelSpacing = xticks_spacing + obj_chart.Axes(AxisType.xlCategory).TickMarkSpacing = xticks_spacing + obj_chart.Axes(AxisType.xlCategory).TickLabelPosition = Constants.xlLow + # user's function (to apply on remaining kwargs) + if customize_func is not None: + customize_func(obj_chart, **customize_kwargs) + # flagflip + nb_xticks = data_range.Rows.Count - 1 + nb_series = data_range.Columns.Count - 1 + if nb_series > 1 and nb_xticks == 1: + obj_chart.PlotBy = RowCol.xlRows + # see above + top_left_cell.Value = top_left_cell_value + return obj_chart # XXX: deprecate this function? def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, app=None, load_addins=None): @@ -756,6 +819,7 @@ def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, a >>> # to create a new Excel file, argument overwrite_file must be set to True >>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP ... wb['arr'] = arr.dump() +... wb['arr']['A1'].make_plot('A6', title='simple graph') ... wb.save() read array from an Excel file diff --git a/larray/tests/test_excel.py b/larray/tests/test_excel.py index ba783032f..a0aa4ab28 100644 --- a/larray/tests/test_excel.py +++ b/larray/tests/test_excel.py @@ -250,6 +250,22 @@ def test_repr(self): 1 3 4 5""" +@needs_xlwings +def test_make_plot(): + demo = load_example_data('demography_eurostat') + population = demo.population + population_be = population['Belgium'] + population_be_nan = population_be.astype(float) + population_be_nan[2013] = nan + + with open_excel(filepath='test_make_plot.xlsx', visible=False, overwrite_file=True) as wb: + sheet = wb[0] + sheet["B2"] = population_be.dump() + sheet["B2"].make_plot("B8") + sheet["B2:F4"].make_plot("L8") + wb.save() + + # ================ # # Test ExcelReport # # ================ # From 34ec5a4f33ace5048a625c77acf0c86c5daa2536 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Tue, 18 Jan 2022 15:16:53 +0100 Subject: [PATCH 2/8] replaced position argument by data_source. syntax is now: sheet[graph_position].make_plot(data_source) --- larray/inout/xw_excel.py | 11 +++++------ larray/tests/test_excel.py | 4 ++-- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/larray/inout/xw_excel.py b/larray/inout/xw_excel.py index c6d92ecc9..afb036d66 100644 --- a/larray/inout/xw_excel.py +++ b/larray/inout/xw_excel.py @@ -620,7 +620,7 @@ def load(self, header=True, convert_float=True, nb_axes=None, index_col=None, fi else: return Array(list_data) - def make_plot(self, position: str, width: int=427, height: int=230, title: str=None, template: str=None, + def make_plot(self, data_source: str, width: int=427, height: int=230, title: str=None, template: str=None, min_y: Union[int, float]=None, max_y: Union[int, float]=None, xticks_spacing: Union[int, float]=None, customize_func: Callable=None, customize_kwargs: Dict[str, str]=None) -> Any: @@ -632,7 +632,7 @@ def make_plot(self, position: str, width: int=427, height: int=230, title: str=N raise ValueError(f"Could not find template file {template}") title = str(title) if title is not None else None sheet = self.sheet.xw_sheet.api - data_range = self.xw_range.api + data_range = sheet.Range(data_source) top_left_cell = data_range.Cells(1, 1) # expand if current range is one cell if data_range.Count == 1: @@ -644,12 +644,11 @@ def make_plot(self, position: str, width: int=427, height: int=230, title: str=N top_left_cell.Value = '' # start chart sheet_charts = sheet.ChartObjects() - position = sheet.Range(position) - left, top = position.Left, position.Top + range_chart = self.xw_range.api + left, top = range_chart.Left, range_chart.Top obj = sheet_charts.Add(left, top, width, height) obj_chart = obj.Chart - source = data_range - obj_chart.SetSourceData(source) + obj_chart.SetSourceData(data_range) obj_chart.ChartType = ChartType.xlLine # title if title is not None: diff --git a/larray/tests/test_excel.py b/larray/tests/test_excel.py index a0aa4ab28..b71eb503e 100644 --- a/larray/tests/test_excel.py +++ b/larray/tests/test_excel.py @@ -261,8 +261,8 @@ def test_make_plot(): with open_excel(filepath='test_make_plot.xlsx', visible=False, overwrite_file=True) as wb: sheet = wb[0] sheet["B2"] = population_be.dump() - sheet["B2"].make_plot("B8") - sheet["B2:F4"].make_plot("L8") + sheet["B8"].make_plot("B2") + sheet["L8"].make_plot("B2:F4") wb.save() From a17554498f2cc9975a061a68f9d182c8430ca061 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Tue, 18 Jan 2022 15:22:08 +0100 Subject: [PATCH 3/8] renamed make_plot() as add_plot() --- doc/source/changes/version_0_33.rst.inc | 2 +- larray/inout/xw_excel.py | 10 +++++----- larray/tests/test_excel.py | 8 ++++---- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/doc/source/changes/version_0_33.rst.inc b/doc/source/changes/version_0_33.rst.inc index b494d7434..f5c56cb3d 100644 --- a/doc/source/changes/version_0_33.rst.inc +++ b/doc/source/changes/version_0_33.rst.inc @@ -65,7 +65,7 @@ Miscellaneous improvements * implemented :py:obj:`Axis.min()` and :py:obj:`Axis.max()` methods (closes :issue:`874`). -* implemented the :py:obj:`Range.make_plot()` method that allows to create graphs when using +* implemented the :py:obj:`Range.add_plot()` method that allows to create graphs when using `open_excel()` (closes :issue:`900`). Fixes diff --git a/larray/inout/xw_excel.py b/larray/inout/xw_excel.py index afb036d66..081204995 100644 --- a/larray/inout/xw_excel.py +++ b/larray/inout/xw_excel.py @@ -620,10 +620,10 @@ def load(self, header=True, convert_float=True, nb_axes=None, index_col=None, fi else: return Array(list_data) - def make_plot(self, data_source: str, width: int=427, height: int=230, title: str=None, template: str=None, - min_y: Union[int, float]=None, max_y: Union[int, float]=None, - xticks_spacing: Union[int, float]=None, customize_func: Callable=None, - customize_kwargs: Dict[str, str]=None) -> Any: + def add_plot(self, data_source: str, width: int=427, height: int=230, title: str=None, template: str=None, + min_y: Union[int, float]=None, max_y: Union[int, float]=None, + xticks_spacing: Union[int, float]=None, customize_func: Callable=None, + customize_kwargs: Dict[str, str]=None) -> Any: from xlwings.constants import LegendPosition, ChartType, RowCol, AxisType, Constants, Direction if customize_func is not None and not callable(customize_func): raise TypeError(f"Expected a function for the argument 'customize_func'. " @@ -818,7 +818,7 @@ def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, a >>> # to create a new Excel file, argument overwrite_file must be set to True >>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP ... wb['arr'] = arr.dump() -... wb['arr']['A1'].make_plot('A6', title='simple graph') +... wb['arr']['A6'].add_plot('A1', title='simple graph') ... wb.save() read array from an Excel file diff --git a/larray/tests/test_excel.py b/larray/tests/test_excel.py index b71eb503e..b3836536b 100644 --- a/larray/tests/test_excel.py +++ b/larray/tests/test_excel.py @@ -251,18 +251,18 @@ def test_repr(self): @needs_xlwings -def test_make_plot(): +def test_add_plot(): demo = load_example_data('demography_eurostat') population = demo.population population_be = population['Belgium'] population_be_nan = population_be.astype(float) population_be_nan[2013] = nan - with open_excel(filepath='test_make_plot.xlsx', visible=False, overwrite_file=True) as wb: + with open_excel(filepath='test_add_plot.xlsx', visible=False, overwrite_file=True) as wb: sheet = wb[0] sheet["B2"] = population_be.dump() - sheet["B8"].make_plot("B2") - sheet["L8"].make_plot("B2:F4") + sheet["B8"].add_plot("B2") + sheet["L8"].add_plot("B2:F4") wb.save() From 8c00ba0f96b872f884aad8484223179c6606fc0a Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Tue, 18 Jan 2022 16:12:49 +0100 Subject: [PATCH 4/8] implemented Range.add_table() --- doc/source/changes/version_0_33.rst.inc | 2 ++ larray/inout/xw_excel.py | 13 ++++++------- larray/tests/test_excel.py | 10 ++++++++++ 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/doc/source/changes/version_0_33.rst.inc b/doc/source/changes/version_0_33.rst.inc index f5c56cb3d..4a841ab2a 100644 --- a/doc/source/changes/version_0_33.rst.inc +++ b/doc/source/changes/version_0_33.rst.inc @@ -68,6 +68,8 @@ Miscellaneous improvements * implemented the :py:obj:`Range.add_plot()` method that allows to create graphs when using `open_excel()` (closes :issue:`900`). +* implemented the :py:obj:`Range.add_table()` method. + Fixes ^^^^^ diff --git a/larray/inout/xw_excel.py b/larray/inout/xw_excel.py index 081204995..f60defaae 100644 --- a/larray/inout/xw_excel.py +++ b/larray/inout/xw_excel.py @@ -620,6 +620,9 @@ def load(self, header=True, convert_float=True, nb_axes=None, index_col=None, fi else: return Array(list_data) + def add_table(self, array: Array): + self.xw_range.value = array.dump() + def add_plot(self, data_source: str, width: int=427, height: int=230, title: str=None, template: str=None, min_y: Union[int, float]=None, max_y: Union[int, float]=None, xticks_spacing: Union[int, float]=None, customize_func: Callable=None, @@ -806,19 +809,15 @@ def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, a Examples -------- ->>> arr = ndtest((3, 3)) ->>> arr -a\b b0 b1 b2 - a0 0 1 2 - a1 3 4 5 - a2 6 7 8 +>>> arr, arr2 = ndtest((3, 3)), ndtest((2, 2)) create a new Excel file and save an array >>> # to create a new Excel file, argument overwrite_file must be set to True >>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP ... wb['arr'] = arr.dump() -... wb['arr']['A6'].add_plot('A1', title='simple graph') +... wb['arr']['A6'].add_table() +... wb['arr']['G1'].add_plot('A1', title='simple graph') ... wb.save() read array from an Excel file diff --git a/larray/tests/test_excel.py b/larray/tests/test_excel.py index b3836536b..63f9fef36 100644 --- a/larray/tests/test_excel.py +++ b/larray/tests/test_excel.py @@ -250,6 +250,16 @@ def test_repr(self): 1 3 4 5""" +@needs_xlwings +def test_add_table(): + arr = ndtest((3, 3)) + + with open_excel(filepath='test_add_table.xlsx', visible=False, overwrite_file=True) as wb: + sheet = wb[0] + sheet["B2"].add_table(arr) + wb.save() + + @needs_xlwings def test_add_plot(): demo = load_example_data('demography_eurostat') From 4f9febc13118b98d8e6f728b425eeef09102be4d Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Tue, 8 Feb 2022 16:38:18 +0100 Subject: [PATCH 5/8] >> updated open_excel() example --- larray/inout/xw_excel.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/larray/inout/xw_excel.py b/larray/inout/xw_excel.py index f60defaae..b17e7737d 100644 --- a/larray/inout/xw_excel.py +++ b/larray/inout/xw_excel.py @@ -815,16 +815,28 @@ def open_excel(filepath=None, overwrite_file=False, visible=None, silent=None, a >>> # to create a new Excel file, argument overwrite_file must be set to True >>> with open_excel('excel_file.xlsx', overwrite_file=True) as wb: # doctest: +SKIP +... # create a new sheet 'arr' and dump the array 'arr' starting at cell A1 ... wb['arr'] = arr.dump() -... wb['arr']['A6'].add_table() -... wb['arr']['G1'].add_plot('A1', title='simple graph') +... +... # dump array 'arr2' starting at cell F1 +... wb['arr']['F1'].add_table(arr2) +... +... # add a plot with left top corner anchored to cell A6 and +... # using data in range A1:D4 +... wb['arr']['A6'].add_plot('A1:D4', title='simple graph') +... +... # add a plot with left top corner anchored to cell F6 and +... # using data in range F1:H3 where H3 is deduced automatically +... wb['arr']['F6'].add_plot('F1', title='second simple graph') +... +... save the workbook ... wb.save() read array from an Excel file >>> with open_excel('excel_file.xlsx') as wb: # doctest: +SKIP -... arr2 = wb['arr'].load() ->>> arr2 # doctest: +SKIP +... arr3 = wb['arr'].load() +>>> arr3 # doctest: +SKIP a\b b0 b1 b2 a0 0 1 2 a1 3 4 5 From 3055aea239ad80c0a79a0857640213e446184981 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Mon, 23 May 2022 15:40:11 +0200 Subject: [PATCH 6/8] updated tutorial_IO.ipynb -> added an example for add_table() --- doc/source/tutorial/tutorial_IO.ipyml | 17 +++++++++++++++-- doc/source/tutorial/tutorial_IO.ipynb | 20 ++++++++++++++++++-- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/doc/source/tutorial/tutorial_IO.ipyml b/doc/source/tutorial/tutorial_IO.ipyml index 9a56bbc42..a57fb6717 100644 --- a/doc/source/tutorial/tutorial_IO.ipyml +++ b/doc/source/tutorial/tutorial_IO.ipyml @@ -319,6 +319,8 @@ cells: sh = wb['population_births_deaths'] # dump the array population in sheet 'population_births_deaths' starting at cell A2 sh['A2'] = population.dump() + # or equivalently + sh['A2'].add_table(population) # add 'births' in cell A10 sh['A10'] = 'births' # dump the array births in sheet 'population_births_deaths' starting at cell A11 @@ -334,6 +336,17 @@ cells: ``` +- markdown: | +
+ **Note:** The two syntaxes: +
    +
  • sheet['cell'] = array.dump()
  • +
  • sheet['cell'].add_table(array)
  • +
+ are equivalent. +
+ + - markdown: | ## Exporting data without headers (Excel) @@ -541,7 +554,7 @@ cells: metadata: celltoolbar: Edit Metadata kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -553,7 +566,7 @@ metadata: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.9.5 livereveal: autolaunch: false scroll: true diff --git a/doc/source/tutorial/tutorial_IO.ipynb b/doc/source/tutorial/tutorial_IO.ipynb index 6026cbcea..ae242fe4a 100644 --- a/doc/source/tutorial/tutorial_IO.ipynb +++ b/doc/source/tutorial/tutorial_IO.ipynb @@ -465,6 +465,8 @@ " sh = wb['population_births_deaths']\n", " # dump the array population in sheet 'population_births_deaths' starting at cell A2\n", " sh['A2'] = population.dump()\n", + " # or equivalently\n", + " sh['A2'].add_table(population)\n", " # add 'births' in cell A10\n", " sh['A10'] = 'births'\n", " # dump the array births in sheet 'population_births_deaths' starting at cell A11 \n", @@ -480,6 +482,20 @@ "```" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "
\n", + " **Note:** The two syntaxes:\n", + "
    \n", + "
  • sheet['cell'] = array.dump()
  • \n", + "
  • sheet['cell'].add_table(array)
  • \n", + "
\n", + " are equivalent.\n", + "
" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -774,7 +790,7 @@ "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -788,7 +804,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" }, "livereveal": { "autolaunch": false, From 3a4166aee81a832f8aaeea8752e01d2c36133803 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Mon, 23 May 2022 16:30:00 +0200 Subject: [PATCH 7/8] updated unittest function test_add_plot() --- larray/tests/test_excel.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/larray/tests/test_excel.py b/larray/tests/test_excel.py index 63f9fef36..fc747f506 100644 --- a/larray/tests/test_excel.py +++ b/larray/tests/test_excel.py @@ -269,10 +269,18 @@ def test_add_plot(): population_be_nan[2013] = nan with open_excel(filepath='test_add_plot.xlsx', visible=False, overwrite_file=True) as wb: - sheet = wb[0] - sheet["B2"] = population_be.dump() - sheet["B8"].add_plot("B2") - sheet["L8"].add_plot("B2:F4") + wb['BFG'] = population_be.dump() + sheet = wb['BFG'] + sheet["H1"].add_plot("A1", title="Belgium") + sheet["R1"].add_plot("A1:F4") + + sheet["A18"].add_table(population['France']) + sheet["H18"].add_plot("A18:E20", title="France") + + sheet["A35"].add_table(population["Germany"]) + sheet["H35"].add_plot("A35", title="Germany", width=500, height=300, xticks_spacing=2, + min_y=40_000_000, max_y=41_500_000) + wb.save() From 759547c2f37bdf0699262c2c7e8939a1c6f60522 Mon Sep 17 00:00:00 2001 From: Alix Damman Date: Mon, 23 May 2022 16:31:23 +0200 Subject: [PATCH 8/8] tutorial: added section Excel Plot --- doc/source/tutorial/tutorial_plotting.ipyml | 46 +++++++++++++++- doc/source/tutorial/tutorial_plotting.ipynb | 58 ++++++++++++++++++++- 2 files changed, 100 insertions(+), 4 deletions(-) diff --git a/doc/source/tutorial/tutorial_plotting.ipyml b/doc/source/tutorial/tutorial_plotting.ipyml index 7dc75ba6b..5288a328a 100644 --- a/doc/source/tutorial/tutorial_plotting.ipyml +++ b/doc/source/tutorial/tutorial_plotting.ipyml @@ -32,6 +32,10 @@ cells: %matplotlib inline +- markdown: | + ## Python Plot (matplotlib) + + - markdown: | In a Python script, add the following import on top of the script: @@ -145,12 +149,50 @@ cells: See [pyplot tutorial](https://matplotlib.org/tutorials/introductory/pyplot.html) for a short introduction to `matplotlib.pyplot`. +- markdown: | + ## Excel Plot + + +- markdown: | + It is possible to dump arrays and to make plots in the same time in an Excel workbook: + + +- markdown: | + ```python + # to create a new Excel file, argument overwrite_file must be set to True + with open_excel('workbook_with_plots.xlsx', overwrite_file=True) as wb: + # ---- dump data + # add a new sheet 'BFG' and dump the data for Belgium in it + wb['BFG'] = population['Belgium'].dump() + # store the BFG sheet in a local variable + sh = wb['BFG'] + # dump the data for France using the equivalent method add_table() + sh['A18'].add_table(population['France']) + # dump the data for Germany + sh['A35'].add_table(population['Germany']) + + # ---- use data to create plots + # basic plot anchored to cell H1 using data for Belgium + sh['H1'].add_plot('A1', title='Belgium population') + # basic plot anchored to cell H18 using data for the France for years 2013 to 2016 + sh['H18'].add_plot('A18:E20', title='France population') + # plot with options anchored to cell H35 using data for Germany + sh["H35"].add_plot("A35", title="Germany population", width=500, height=300, + xticks_spacing=2, min_y=40_000_000, max_y=41_500_000) + + # actually write data and plots in the Workbook + wb.save() + + # the Workbook is automatically closed when getting out the block defined by the with statement + ``` + + # The lines below here may be deleted if you do not need them. # --------------------------------------------------------------------------- metadata: celltoolbar: Edit Metadata kernelspec: - display_name: Python 3 + display_name: Python 3 (ipykernel) language: python name: python3 language_info: @@ -162,7 +204,7 @@ metadata: name: python nbconvert_exporter: python pygments_lexer: ipython3 - version: 3.7.3 + version: 3.9.5 livereveal: autolaunch: false scroll: true diff --git a/doc/source/tutorial/tutorial_plotting.ipynb b/doc/source/tutorial/tutorial_plotting.ipynb index fff1cbafe..65e9c5edf 100644 --- a/doc/source/tutorial/tutorial_plotting.ipynb +++ b/doc/source/tutorial/tutorial_plotting.ipynb @@ -59,6 +59,13 @@ "%matplotlib inline" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Python Plot (matplotlib)" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -246,12 +253,59 @@ "\n", "See [pyplot tutorial](https://matplotlib.org/tutorials/introductory/pyplot.html) for a short introduction to `matplotlib.pyplot`." ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Excel Plot" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "It is possible to dump arrays and to make plots in the same time in an Excel workbook:" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "```python\n", + "# to create a new Excel file, argument overwrite_file must be set to True\n", + "with open_excel('workbook_with_plots.xlsx', overwrite_file=True) as wb:\n", + " # ---- dump data\n", + " # add a new sheet 'BFG' and dump the data for Belgium in it \n", + " wb['BFG'] = population['Belgium'].dump()\n", + " # store the BFG sheet in a local variable\n", + " sh = wb['BFG']\n", + " # dump the data for France using the equivalent method add_table()\n", + " sh['A18'].add_table(population['France'])\n", + " # dump the data for Germany\n", + " sh['A35'].add_table(population['Germany'])\n", + " \n", + " # ---- use data to create plots\n", + " # basic plot anchored to cell H1 using data for Belgium\n", + " sh['H1'].add_plot('A1', title='Belgium population')\n", + " # basic plot anchored to cell H18 using data for the France for years 2013 to 2016\n", + " sh['H18'].add_plot('A18:E20', title='France population')\n", + " # plot with options anchored to cell H35 using data for Germany\n", + " sh[\"H35\"].add_plot(\"A35\", title=\"Germany population\", width=500, height=300, \n", + " xticks_spacing=2, min_y=40_000_000, max_y=41_500_000)\n", + " \n", + " # actually write data and plots in the Workbook\n", + " wb.save()\n", + " \n", + "# the Workbook is automatically closed when getting out the block defined by the with statement\n", + "```" + ] } ], "metadata": { "celltoolbar": "Edit Metadata", "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -265,7 +319,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.7.3" + "version": "3.9.5" }, "livereveal": { "autolaunch": false,