Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a duplicate of the dictionary for the default colorbar parameters of plot2D #2380

Merged
merged 1 commit into from
Jan 24, 2023

Conversation

oskooi
Copy link
Collaborator

@oskooi oskooi commented Jan 23, 2023

The colorbar feature for plot2D added in #2289 produces an error when trying to add colorbars to more than one subplots of a single matplotlib.figure object. This is because the dictionary of default parameters for the colorbar (default_colorbar_parametrs of visualization.py) is passed by reference and the use of pop to retrieve its parameters when adding the colorbar to the first subplot removes them from the dictionary. Subsequent calls to retrieve the same parameters in turn triggers an error. The fix involves simply creating a duplicate of the dictionary.

Here is an example which demonstrates this bug:

""                                                                                                               
Plots the fields for an Er point source at r=z=0 in vacuum.                                                       
"""

import meep as mp
import numpy as np
import matplotlib
matplotlib.use('agg')
import matplotlib.pyplot as plt


res = 50
dpml = 1.0
s = 5.0
m = -1
fcen = 1.

cell_size = mp.Vector3(s+dpml,0,s+2*dpml)

pml_layers = [
    mp.PML(dpml, direction=mp.R),
    mp.PML(dpml, direction=mp.Z),
]

sources = [
    mp.Source(
	src=mp.ContinuousSource(fcen),
        center=mp.Vector3(2.3),
        component=mp.Er,
    ),
]

sim = mp.Simulation(
    resolution=res,
    cell_size=cell_size,
    dimensions=mp.CYLINDRICAL,
    m=m,
    sources=sources,
    boundary_layers=pml_layers,
)

sim.run(until=26.5)

fig, ax = plt.subplots(ncols=2)
fig.subplots_adjust(wspace=0.7)

sim.plot2D(
    ax=ax[0],
    fields=mp.Er,
    field_parameters={
	'colorbar':True,
        'cmap':'inferno',
        'post_process':lambda x: np.log10(np.abs(np.real(x))),
    },
)
ax[0].set_title("$|\Re(E_r)|$")

sim.plot2D(
    ax=ax[1],
    fields=mp.Er,
    field_parameters={
        'colorbar':True,
        'cmap':'inferno',
	'post_process':lambda x: np.log10(np.abs(np.imag(x))),
    },
)
ax[1].set_title("$|\Im(E_r)|$")

if mp.am_master():
    plt.savefig(
        f'cyl_er_real_imag.png',
        dpi=150,
        bbox_inches='tight'
    )

Executing this script using the current master branch triggers an error related to a missing pad key entry in the dictionary when trying to add a colorbar to the second subplot:

Traceback (most recent call last):
  File "bug_colorbar.py", line 58, in <module>
    sim.plot2D(
  File "/meep/python/meep/simulation.py", line 4680, in plot2D
    return vis.plot2D(
  File "/meep/python/meep/visualization.py", line 977, in plot2D
    ax = plot_fields(
  File "/meep/python/meep/visualization.py", line 885, in plot_fields
    _add_colorbar(
  File "/meep/python/meep/visualization.py", line 491, in _add_colorbar
    pad=colorbar_parameters.pop("pad"),
KeyError: 'pad'

With the fix in this PR, colorbars are correctly added to both subplots as shown in the following image:

cyl_er_real_imag

cc @thomasdorch

@oskooi oskooi added the bug label Jan 23, 2023
@codecov-commenter
Copy link

Codecov Report

Merging #2380 (a03a053) into master (f448ebd) will decrease coverage by 0.03%.
The diff coverage is 16.66%.

@@            Coverage Diff             @@
##           master    #2380      +/-   ##
==========================================
- Coverage   72.59%   72.57%   -0.03%     
==========================================
  Files          17       17              
  Lines        5163     5166       +3     
==========================================
+ Hits         3748     3749       +1     
- Misses       1415     1417       +2     
Impacted Files Coverage Δ
python/visualization.py 36.66% <16.66%> (-0.02%) ⬇️

@stevengj stevengj merged commit 85e1077 into NanoComp:master Jan 24, 2023
@oskooi oskooi deleted the plot2D_colorbar_bug branch January 24, 2023 17:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants