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

Possible bug: eps_writer doesn't retain formatting of SlicePlot object #4803

Closed
dunhamsj opened this issue Feb 2, 2024 · 6 comments
Closed

Comments

@dunhamsj
Copy link

dunhamsj commented Feb 2, 2024

Bug report

Bug summary

Saving a figure using yt.visualization.eps_writer.single_plot( slc ).save_fig( 'fig', format = 'pdf' ) causes some of the formatting of the original SlicePlot to be lost. In particular, ticks and all three axis labels are different and the origin parameter seems to be reset.

Code for reproduction

#!/usr/bin/env python3

import yt

# Taken from the yt data hub
file = 'castro_sedov_2d_cyl_in_cart_plt00150'
ds = yt.load( file )

bl = 'gas'
Field = 'density'
blField = (bl,Field)

slc \
  = yt.SlicePlot \
      ( ds, 'z', blField, \
        origin = 'native' )

slc.set_log( Field, log = True )

slc.set_axes_unit( [ 'code_length', 'code_length' ] )

slc.set_xlabel( r'$x$' )
slc.set_ylabel( r'$y$' )

slc.set_colorbar_label( Field, r'$\rho$' )

slc.save()

import yt.visualization.eps_writer as eps

eps_fig = eps.single_plot( slc )
eps_fig.save_fig( 'fig', format = 'pdf' )

Actual outcome

2024-02-02--165146

Expected outcome
2024-02-02--165541

Version Information

  • Operating System: arch linux
  • Python Version: 3.11.6
  • yt version: 4.3.0

I installed python with pacman -S python, made a virtual environment, and installed yt to the virtual environment.

Copy link

welcome bot commented Feb 2, 2024

Hi, and welcome to yt! Thanks for opening your first issue. We have an issue template that helps us to gather relevant information to help diagnosing and fixing the issue.

@chrishavlin
Copy link
Contributor

Does slc.save('fig_slc_save.pdf', suffix="pdf") not work correctly?

The eps_writer does a bunch of default decision making for you that is hard to override in single_plot. You can, however, use the DualEPS class (that is used by single_plot to make an almost identical figure after you've built your slc:

from yt.visualization.eps_writer import DualEPS
import pyx

slc.refresh()

d = DualEPS()
d.insert_image_yt(slc, field=Field)

# use `axis_box` instead of `axis_box_yt` to avoid the xlim, ylim re-scaling
d.axis_box(
            xrange=[slc.xlim[i] for i in (0, 1)],
            yrange=[slc.ylim[i] for i in (0, 1)],
            xlabel=slc.plots[Field].axes.get_xlabel(),
            ylabel=slc.plots[Field].axes.get_ylabel(),
            tickcolor=pyx.color.cmyk.black,
            xlog=False,
            ylog=False,
            bare_axes=False,
        )

# use `colorbar` instead of `colorbar_yt` to avoid adding units to
# the colorbar label.
_p = slc.plots[Field]
_norm = _p.norm_handler.get_norm(slc.frb[Field])

d.colorbar(slc[Field].colorbar_handler.cmap, 
           zrange=(_norm.vmin, _norm.vmax), 
           label=slc.frb[Field].info["label"], 
           log=True)

d.save_fig('fig_manual', format='pdf')

fig_manual.pdf

The only thing I can't manage to match exactly is the colobar labels -- it seems PyX (the package yt is using to generate the eps) will automaticaly switch to scientific notation when the smallest value is < 1e-3 or the largest value is > 1e3, but even then it will decide where to place colorbar tick labels and so won't match yt's matplotlib style exactly.

@dunhamsj
Copy link
Author

dunhamsj commented Feb 7, 2024

Ah, I didn't know about the slc.save('fig_slc_save.pdf', suffix="pdf"). That did it! I couldn't find that in the this documentation. It may be worth adding that in. Thank you for your help! I knew it was easier than I was trying to make it, as is so often the case.

@dunhamsj dunhamsj closed this as completed Feb 7, 2024
@neutrinoceros
Copy link
Member

@dunhamsj I'm not sure I get it. The .save method is used extensively in docs, including (seems to me) in the page you just linked.

@dunhamsj
Copy link
Author

dunhamsj commented Feb 7, 2024

It may be, and I just missed it. I was making this figure for a publication and so I went to the section of the docs that referred to "Publication-Ready Figures", and that section didn't mention the method that chrishaviln suggested. The docs seemed to rely on the eps_writer method

@neutrinoceros
Copy link
Member

Ah well, I guess that section title didn't age very well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants