Internal

Useful information for the research group

Python tips and tricks

Remove white lines in filled contour plots, pcolormesh plots, and colorbars

PDF output from Matplotlib often has little white lines separating patches in contour and pcolormesh plots, as well as colorbars. This will remove them from contour plots:

for c in cs.collections:
    c.set_edgecolor('face')

This does the same thing for pcolormesh plots:

pc.set_edgecolor('face')

This does the same thing for colorbars:

cbar.solids.set_edgecolor('face')

cs, pc, and cbar are handles to the contour plot, pcolormesh plot, and colorbar objects.

Make figure background transparent while keeping axis backgrounds:

fig.patch.set_alpha(0.0)

Working with meta

Development environments

To activate various development environments, type the following at the bash prompt:

  • Python 3.9: type mamba activate latest

This will selection will only last as long as your login session. If you log out, you will need to reactive the desired environment.

Jupyter Lab

To run a Jupyter notebook server that will close when you close your terminal:

  1. Activate Python using conda activate
  2. Type jupyter lab
  3. The notebook server will tell you The Jupyter Notebook is running at: http://localhost:YYYY/, where YYYY is your port number.
  4. Open an SSH tunnel from your local machine to meta using the port YYYY. On Mac or Linux, this can be done by typing ssh -L YYYY:localhost:YYYY meta.somas.stonybrook.edu on your local machine.
  5. Point your browser to http://localhost:YYYY. Your browser may warn you that the connection is not secure. Don’t worry about this and create a security exception for meta on your browser.
  6. To shut down the notebook server, either close your terminal or type Control-C in the terminal window you used to start the server.

To run a Jupyter notebook server that will not close when you close your terminal:

  1. Activate Python using conda activate
  2. Type nohup jupyter lab &. ‘nohup’ tells the shell not to kill the server when you close the terminal.
  3. The first line after you press enter will have the format [#] XXXXX, where # is a number and XXXXX is the process ID (PID) number.
  4. The message telling you the server port will be at the bottom of the file nohup.out. You can display the last few lines of this file by typing tail nohup.out.
  5. Open a SSH tunnel and use your browser as described above.
  6. To shut down the notebook server, type kill XXXXX on the bash prompt. If you forgot your PID, use ps to list the PIDs of your running processes. After you kill the server, delete nohup.out or else it will keep growing every time you restart the server.