# Plotting error, grid\_b

**URL:** <https://openturns.discourse.group/t/plotting-error-grid-b/291>\
**Category:** Python usage\
**Created:** [May 24, 2023, 3:47pm UTC](https://openturns.discourse.group/t/plotting-error-grid-b/291 "2023-05-24T15:47:35Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PhilippIce](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/philippice/32/263_2.png) [@PhilippIce](https://openturns.discourse.group/u/PhilippIce)\
**Post date:** [May 24, 2023, 3:47pm UTC](https://openturns.discourse.group/t/plotting-error-grid-b/291/1 "2023-05-24T15:47:35Z")

</div>

Hey Everybody,  
Thanks for the awesome software.  
I seem to be running into a bug with incompatible versions of the software.  
I am running openturns version 1.20.post3 and matplotlib 3.7.1.  
When I am trying to plot something with viewer, there seems to be a bug.

Here is a small mwe

```python
import openturns as ot
import openturns.viewer as viewer
sampleSize=10
g = ot.SymbolicFunction(["x"], ["0.5*x^2 + sin(2.5*x)"])
xMin = -0.9
xMax = 1.9
X_distr = ot.Uniform(xMin, xMax)
X = ot.LHSExperiment(X_distr, sampleSize, False, False).generate()
Y = g(X)

graph = g.draw(xMin, xMax)
view = viewer.View(graph)

```

This produces the following errror:

```auto
NameError Traceback (most recent call last)
Cell In[9], line 7
      5 xMax = 1.9
      6 X_distr = ot.Uniform(xMin, xMax)
----> 7 X = ot.LHSExperiment(X_distr, sampleSize, False, False).generate()
      8 Y = g(X)
     10 graph = g.draw(xMin, xMax)

NameError: name 'sampleSize' is not defined

In [10]: 
    ...: 
    ...: import openturns as ot
    ...: import openturns.viewer as viewer
    ...: sampleSize=10
    ...: g = ot.SymbolicFunction(["x"], ["0.5*x^2 + sin(2.5*x)"])
    ...: xMin = -0.9
    ...: xMax = 1.9
    ...: X_distr = ot.Uniform(xMin, xMax)
    ...: X = ot.LHSExperiment(X_distr, sampleSize, False, False).generate()
    ...: Y = g(X)
    ...: 
    ...: graph = g.draw(xMin, xMax)
    ...: view = viewer.View(graph)
    ...: 
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[10], line 12
      9 Y = g(X)
     11 graph = g.draw(xMin, xMax)
---> 12 view = viewer.View(graph)

File ~/miniconda3/envs/kriging3/lib/python3.11/site-packages/openturns/viewer.py:299, in View. __init__ (self, graph, pixelsize, figure, figure_kw, axes, plot_kw, axes_kw, bar_kw, pie_kw, polygon_kw, polygoncollection_kw, contour_kw, step_kw, clabel_kw, text_kw, legend_kw, add_legend, square_axes, **kwargs)
    294 if (graph.getLogScale() == ot.GraphImplementation.LOGY) or (
    295 graph.getLogScale() == ot.GraphImplementation.LOGXY
    296 ):
    297 self._ax[0].set_yscale("log")
--> 299 self._ax[0].grid(b=graph.getGrid())
    301 # use scientific notation on non-log axis
    302 axis_mask = {
    303 ot.GraphImplementation.NONE: "both",
    304 ot.GraphImplementation.LOGX: "y",
    305 ot.GraphImplementation.LOGY: "x",
    306 ot.GraphImplementation.LOGXY: None,
    307 }

File ~/miniconda3/envs/kriging3/lib/python3.11/site-packages/matplotlib/axes/_base.py:3194, in _AxesBase.grid(self, visible, which, axis, **kwargs)
   3192 _api.check_in_list(['x', 'y', 'both'], axis=axis)
   3193 if axis in ['x', 'both']:
-> 3194 self.xaxis.grid(visible, which=which, **kwargs)
   3195 if axis in ['y', 'both']:
   3196 self.yaxis.grid(visible, which=which, **kwargs)

File ~/miniconda3/envs/kriging3/lib/python3.11/site-packages/matplotlib/axis.py:1660, in Axis.grid(self, visible, which, **kwargs)
   1657 if which in ['major', 'both']:
   1658 gridkw['gridOn'] = (not self._major_tick_kw['gridOn']
   1659 if visible is None else visible)
-> 1660 self.set_tick_params(which='major', **gridkw)
   1661 self.stale = True

File ~/miniconda3/envs/kriging3/lib/python3.11/site-packages/matplotlib/axis.py:932, in Axis.set_tick_params(self, which, reset, **kwargs)
    919 """
    920 Set appearance parameters for ticks, ticklabels, and gridlines.
    921 
   (...)
    929 gridlines.
    930 """
    931 _api.check_in_list(['major', 'minor', 'both'], which=which)
--> 932 kwtrans = self._translate_tick_params(kwargs)
    934 # the kwargs are stored in self._major/minor_tick_kw so that any
    935 # future new ticks will automatically get them
    936 if reset:

File ~/miniconda3/envs/kriging3/lib/python3.11/site-packages/matplotlib/axis.py:1076, in Axis._translate_tick_params(kw, reverse)
   1074 for key in kw_:
   1075 if key not in allowed_keys:
-> 1076 raise ValueError(
   1077 "keyword %s is not recognized; valid keywords are %s"
   1078 % (key, allowed_keys))
   1079 kwtrans.update(kw_)
   1080 return kwtrans

ValueError: keyword grid_b is not recognized; valid keywords are ['size', 'width', 'color', 'tickdir', 'pad', 'labelsize', 'labelcolor', 'zorder', 'gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On', 'length', 'direction', 'left', 'bottom', 'right', 'top', 'labelleft', 'labelbottom', 'labelright', 'labeltop', 'labelrotation', 'grid_agg_filter', 'grid_alpha', 'grid_animated', 'grid_antialiased', 'grid_clip_box', 'grid_clip_on', 'grid_clip_path', 'grid_color', 'grid_dash_capstyle', 'grid_dash_joinstyle', 'grid_dashes', 'grid_data', 'grid_drawstyle', 'grid_figure', 'grid_fillstyle', 'grid_gapcolor', 'grid_gid', 'grid_in_layout', 'grid_label', 'grid_linestyle', 'grid_linewidth', 'grid_marker', 'grid_markeredgecolor', 'grid_markeredgewidth', 'grid_markerfacecolor', 'grid_markerfacecoloralt', 'grid_markersize', 'grid_markevery', 'grid_mouseover', 'grid_path_effects', 'grid_picker', 'grid_pickradius', 'grid_rasterized', 'grid_sketch_params', 'grid_snap', 'grid_solid_capstyle', 'grid_solid_joinstyle', 'grid_transform', 'grid_url', 'grid_visible', 'grid_xdata', 'grid_ydata', 'grid_zorder', 'grid_aa', 'grid_c', 'grid_ds', 'grid_ls', 'grid_lw', 'grid_mec', 'grid_mew', 'grid_mfc', 'grid_mfcalt', 'grid_ms']

```

Does someone have an idea how I can fix that?

Thanks for your help!

Philipp

---

<div class="post-metadata">

**Author:** ![regislebrun](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/regislebrun/32/154_2.png) [@regislebrun](https://openturns.discourse.group/u/regislebrun)\
**Post date:** [May 26, 2023, 3:44pm UTC](https://openturns.discourse.group/t/plotting-error-grid-b/291/2 "2023-05-26T15:44:38Z")

</div>

Hi,  
This bug is known, see

> <https://github.com/openturns/openturns/issues/2269>
>
> Trying to instantiate the Viewer() with a sample raises an error in line 299 of… viewer.py: \`ValueError: keyword grid\_b is not recognized; \`
> 
> Commenting the line seems to solve the issue.

and

> <https://github.com/openturns/openturns/issues/2240>
>
> The \`Viewer\` sometimes fails with an exception generated by the grid. 
> 
> The fo…llowing script works on my machine. It creates a \`Grid\` based on a function which has a vector output.
> 
> \`\`\`python
> import openturns as ot
> import openturns.viewer as otv
> import numpy as np
> 
> t\_min = 1.e-10
> t\_max = 1.0
> n\_times = 100
> timeGrid = np.linspace(t\_min, t\_max, n\_times)
> 
> g\_function = ot.SymbolicFunction(\["t"\], \["sin(10 \* t) \* t", "sin(1 / t) \* t"\])
> timeSample = ot.Sample.BuildFromPoint(timeGrid)
> result = g\_function(timeSample)
> 
> grid = ot.GridLayout(1, 2)
> graph = ot.Graph("", "t (s)", "T (°C)", True)
> curve = ot.Curve(timeSample, result\[:, 0\])
> graph.add(curve)
> grid.setGraph(0, 0, graph)
> graph = ot.Graph("", "t (s)", "c", True)
> curve = ot.Curve(timeSample, result\[:, 1\])
> graph.add(curve)
> grid.setGraph(0, 1, graph)
> grid.setTitle("Frasil dynamics")
> view = otv.View(grid, figure\_kw={"figsize": (7.0, 3.0)})
> \`\`\`
> On my machine, I use Matplotlib 3.5.2 on top of Python 3.10.
> But CircleCI uses Matplotlib version 3.7.0 and it fails:
> \`\`\`
> ValueError: keyword grid\_b is not recognized; valid keywords are \['size', 'width', 'color', etc.\]
> \`\`\`
> 
> On top of the error stack, there is the Viewer:
> \`\`\`
> File ~/project/AS-Frasil/viewer.py:295, in View.\_\_init\_\_(self, graph, pixelsize, figure, figure\_kw, axes, plot\_kw, axes\_kw, bar\_kw, pie\_kw, polygon\_kw, polygoncollection\_kw, contour\_kw, step\_kw, clabel\_kw, text\_kw, legend\_kw, add\_legend, square\_axes, \*\*kwargs)
> 290 if (graph.getLogScale() == ot.GraphImplementation.LOGY) or (
> 291 graph.getLogScale() == ot.GraphImplementation.LOGXY
> 292 ):
> 293 self.\_ax\[0\].set\_yscale("log")
> \--\> 295 self.\_ax\[0\].grid(b=graph.getGrid())
> 297 # use scientific notation on non-log axis
> 298 axis\_mask = {
> 299 ot.GraphImplementation.NONE: "both",
> 300 ot.GraphImplementation.LOGX: "y",
> 301 ot.GraphImplementation.LOGY: "x",
> 302 ot.GraphImplementation.LOGXY: None,
> 303 }
> \`\`\`
> I cannot reproduce the bug. However, my guess is that the faulty line is:
> 
> https://github.com/openturns/openturns/blob/986ec5b95d30a9492ad6bb8da85fc97240c86608/python/src/viewer.py#L299
> 
> I cannot explain why there is a \`b = graph.getGrid()\` input argument to Matplotlib's \`grid\` function. In my opinion, this should be:
> \`\`\`python
> self.\_ax\[0\].grid(graph.getGrid())
> \`\`\`

It has been fixed in OpenTURNS 1.21rc1, and if you can’t upgrade a workaround is to deactivate the grid in your graph before to send it to the viewer:

```auto
myGraph.setGrid(False)

```

Cheers

Régis

---

<div class="post-metadata">

**Author:** ![PhilippIce](https://yyz2.discourse-cdn.com/free1/user_avatar/openturns.discourse.group/philippice/32/263_2.png) [@PhilippIce](https://openturns.discourse.group/u/PhilippIce)\
**Post date:** [May 30, 2023, 7:40am UTC](https://openturns.discourse.group/t/plotting-error-grid-b/291/3 "2023-05-30T07:40:49Z")

</div>

Hi Régis,  
Thank you very much, sorry, I should’ve checked the Github repo more carefully.

Perfect, now everything looks fine!

Best,

Philipp
