In this code, i specify log tick levels, but this takes no effect. This can be due to…
- User
In this code, i specify log tick levels, but this takes no effect. This can be due to interaction with WCS (see tick fixes in the code). Try to fix the issue.
'''!!! СДЕЛАТЬ RESHAPE !!!''' from matplotlib.ticker import LogLocator fig = plt.figure(figsize=(8, 4)) plt.rcParams['text.usetex'] = True
Create the first subplot
ax1 = fig.add_subplot(121, projection=Ha_cut.wcs) im1 = ax1.imshow(binned_ratio_SII, vmin=1.5, vmax=150, norm='log') ax1.contour(np.log10(Ha), cmap='Reds', linewidths=1, levels=(-10, 2, 2.5, 3, 3.5, 4)) ax1.invert_yaxis() ax1.set_xlabel('RA') ax1.set_ylabel('DEC') ax1.set_title('[O III]/[S II]') ax1.set_title(r'[O,\textsc{iii}] / [S,\textsc{ii}]', pad=35) ax1.invert_yaxis()
Create consistent colorbar for ax1
divider1 = make_axes_locatable(ax1) cax1 = divider1.append_axes('top', size='5%', pad=0.05) cax1.tick_params(labelsize=8, axis='y', direction='in', labelleft=False, left=False, right=False) cax1.tick_params(labelsize=8, axis='x', direction='out', labeltop=True, labelbottom=False, top=True)
cb1 = fig.colorbar(im1, cax=cax1, orientation='horizontal') cb1.set_ticks([2, 10, 30, 100]) # Choose appropriate values on a log scale cb1.ax.xaxis.set_major_locator(LogLocator(base=50, subs='auto')) # Optional: fine-tune
Create the second subplot
ax2 = fig.add_subplot(122, projection=Ha_cut.wcs, sharey=ax1, sharex=ax1) # Share y-axis to ensure alignment im2 = ax2.imshow(binned_ratio_Ha, vmin=1, vmax=12) ax2.contour(np.log10(Ha), cmap='Reds', linewidths=1, levels=(-10, 2, 2.5, 3, 3.5, 4)) ax2.set_xlabel('RA')
ax2.set_title('[O III]/Ha', pad = 30)
ax2.set_title(r'[O,\textsc{iii}] / H', pad=35)
Create consistent colorbar for ax2
divider2 = make_axes_locatable(ax2) cax2 = divider2.append_axes('top', size='5%', pad=0.05) cax2.tick_params(labelsize=8, axis='y', direction='in', labelleft=False, left=False, right=False) cax2.tick_params(labelsize=8, axis='x', direction='out', labeltop=True, labelbottom=False, top=True) cax2.xaxis.get_offset_text().set_visible(False) # Hide offset label (if any) cax1.coords[0].set_auto_axislabel(False) # Disable WCS axis labeling on the x-axis cax1.coords[1].set_auto_axislabel(False)
cax2.coords[0].set_auto_axislabel(False) # Disable WCS axis labeling on the x-axis cax2.coords[1].set_auto_axislabel(False)
ax2.coords[1].set_auto_axislabel(False) # Disable WCS axis labeling on the x-axis ax2.coords[1].set_ticklabel_visible(False) # Hide y-axis tick labels ax2.invert_yaxis()cax2.coords[1].set_auto_axislabel(False)
cb2 = fig.colorbar(im2, cax=cax2, orientation='horizontal')
cb2.set_ticks()
Align the axes limits explicitly
ax1.set_xlim(350, 550)
ax1.set_ylim(700, 500)
ax2.set_xlim(350, 550)
ax2.set_ylim(700, 500)
plt.tight_layout() plt.savefig('OIII_Ha.pdf', dpi=300)
- Assistant
The issue arises because when using
make_axes_locatablewith aWCSAxes(ax1andax2in your case), the appended axes (cax1andcax2) inherit the projection and becomeWCSAxesas well. This can interfere with standard Matplotlib functionalities like setting ticks, especially when dealing with colorbars.To fix this, you need to ensure that the colorbar axes (
cax1andcax2) are standard MatplotlibAxes, notWCSAxes. You can achieve this by specifying theaxes_classparameter when usingappend_axes:cax1 = divider1.append_axes('top', size='5%', pad=0.05, axes_class=plt.Axes)Additionally, when you're using a logarithmic normalization (
norm='log'), it's important to useLogNormfrommatplotlib.colors, as'log'is not a valid string for thenormparameter inimshow.Here's the corrected and updated code:
import matplotlib.pyplot as plt from mpl_toolkits.axes_grid1.axes_divider import make_axes_locatable from matplotlib.colors import LogNorm from matplotlib.ticker import LogLocator, LogFormatter plt.rcParams['text.usetex'] = True fig = plt.figure(figsize=(8, 4)) # Create the first subplot ax1 = fig.add_subplot(121, projection=Ha_cut.wcs) im1 = ax1.imshow(binned_ratio_SII, vmin=1.5, vmax=150, norm=LogNorm(vmin=1.5, vmax=150)) ax1.contour(np.log10(Ha), cmap='Reds', linewidths=1, levels=(-10, 2, 2.5, 3, 3.5, 4)) ax1.invert_yaxis() ax1.set_xlabel('RA') ax1.set_ylabel('DEC') ax1.set_title(r'[O\,\textsc{iii}] / [S\,\textsc{ii}]', pad=35) # Create consistent colorbar for ax1 divider1 = make_axes_locatable(ax1) cax1 = divider1.append_axes('top', size='5%', pad=0.05, axes_class=plt.Axes) cb1 = fig.colorbar(im1, cax=cax1, orientation='horizontal') # Set logarithmic ticks on the colorbar cb1.ax.xaxis.set_major_locator(LogLocator(base=10)) cb1.ax.xaxis.set_major_formatter(LogFormatter()) cb1.ax.xaxis.set_minor_locator(LogLocator(base=10, subs='auto', numticks=10)) cb1.ax.tick_params(labelsize=8, axis='x', direction='out', labeltop=True, labelbottom=False, top=True) cb1.ax.xaxis.get_offset_text().set_visible(False) # Hide offset label if present # Remove the WCS tick labels if any (since cax1 is now a standard Axes, this should be unnecessary) # cax1.coords[0].set_auto_axislabel(False) # cax1.coords[1].set_auto_axislabel(False) # Create the second subplot ax2 = fig.add_subplot(122, projection=Ha_cut.wcs, sharex=ax1, sharey=ax1) im2 = ax2.imshow(binned_ratio_Ha, vmin=1, vmax=12) ax2.contour(np.log10(Ha), cmap='Reds', linewidths=1, levels=(-10, 2, 2.5, 3, 3.5, 4)) ax2.set_xlabel('RA') ax2.set_title(r'[O\,\textsc{iii}] / H$\beta$', pad=35) ax2.coords[1].set_auto_axislabel(False) # Disable WCS axis labeling on the y-axis ax2.coords[1].set_ticklabel_visible(False) # Hide y-axis tick labels ax2.invert_yaxis() # Create consistent colorbar for ax2 divider2 = make_axes_locatable(ax2) cax2 = divider2.append_axes('top', size='5%', pad=0.05, axes_class=plt.Axes) cb2 = fig.colorbar(im2, cax=cax2, orientation='horizontal') # Customize colorbar ticks for the second plot if needed cb2.ax.xaxis.set_major_locator(plt.MaxNLocator(integer=True)) cb2.ax.tick_params(labelsize=8, axis='x', direction='out', labeltop=True, labelbottom=False, top=True) cb2.ax.xaxis.get_offset_text().set_visible(False) # Hide offset label if present # Adjust layout and save the figure plt.tight_layout() plt.savefig('OIII_Ha.pdf', dpi=300)Explanation of Changes:
-
Use
LogNormfor Logarithmic Scaling:- Changed
norm='log'tonorm=LogNorm(vmin=1.5, vmax=150)inimshow. Thenormparameter requires an instance ofNormalizeor its subclasses, andLogNormis appropriate for logarithmic scaling.
- Changed
-
Ensure Colorbar Axes are Standard
Axes:- Specified
axes_class=plt.Axeswhen creatingcax1andcax2usingappend_axes. This prevents the appended axes from inheriting the WCS projection of the main axes.
- Specified
-
Set Logarithmic Ticks on the Colorbar:
- Used
LogLocatorandLogFormatterto set major and minor ticks on the colorbar axes. - Adjusted tick parameters to display the ticks correctly.
- Used
-
Removed Unnecessary WCS Configurations on Colorbar Axes:
- Since
cax1andcax2are now standardAxes, there's no need to manipulatecoordsor WCS-related settings.
- Since
-
Adjusted Ticks for the Second Colorbar:
- For
cb2, customized the ticks usingMaxNLocatorto display integer ticks, if necessary.
- For
-
Ensured Consistency in Tick Parameters:
- Set consistent tick parameters for both colorbars to maintain a uniform appearance.
Note:
- By ensuring
cax1andcax2are standardAxes, standard Matplotlib functions for setting ticks and labels work as expected. - The use of
LogNormensures that the color mapping is correctly logarithmic, and the colorbar reflects this scaling.
Additional Tips:
- Always verify that the normalization (
norm) is set correctly when working with logarithmic data. - When working with WCS projections, be mindful of how appended axes may inherit properties. Specifying the axes class can prevent unexpected behaviors.
- Customize tick locators and formatters to control the appearance of ticks on both plots and colorbars.
-