From 78c3bac24bb38b0406a9ff268fee27d3d88481bf Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Mon, 4 Nov 2024 14:38:09 -0800 Subject: [PATCH 1/9] changes some plot styles for the polar view --- hexrdgui/image_canvas.py | 70 ++++++++++++++++++++++++++++++++++------ 1 file changed, 60 insertions(+), 10 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 2c5e9bcb8..746c924bc 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -8,7 +8,7 @@ from matplotlib.figure import Figure from matplotlib.lines import Line2D from matplotlib.patches import Circle -from matplotlib.ticker import AutoLocator, FuncFormatter +from matplotlib.ticker import AutoLocator, AutoMinorLocator, FuncFormatter import matplotlib.pyplot as plt import matplotlib.transforms as tx @@ -1004,6 +1004,7 @@ def show_polar(self): worker.signals.error.connect(self.async_worker_error) def finish_show_polar(self, iviewer): + if self.mode != ViewType.polar: # Image mode was switched during generation. Ignore this. return @@ -1034,10 +1035,36 @@ def finish_show_polar(self, iviewer): } self.axes_images.append(self.axis.imshow(**kwargs)) self.axis.axis('auto') + + self.axis.yaxis.set_major_locator(AutoLocator()) + self.axis.yaxis.set_minor_locator(AutoMinorLocator()) + + self.axis.xaxis.set_major_locator(AutoLocator()) + self.axis.xaxis.set_minor_locator(AutoMinorLocator()) + + kwargs = { + 'left': True, + 'right': True, + 'bottom': True, + 'top': True, + 'which': 'major', + 'length': 10, + 'labelfontfamily': 'serif', + 'labelsize': 14 + } + self.axis.tick_params(**kwargs) + + kwargs['which'] = 'minor' + kwargs['length'] = 2 + self.axis.tick_params(**kwargs) + + self.axis.tick_params(bottom=True, top=True, which='major', length=8) + self.axis.tick_params(bottom=True, top=True, which='minor', length=2) + # Do not allow the axis to autoscale, which could happen if # overlays are drawn out-of-bounds self.axis.autoscale(False) - self.axis.set_ylabel(r'$\eta$ [deg]') + self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=14, family='serif') self.axis.label_outer() else: rescale_image = False @@ -1053,11 +1080,11 @@ def finish_show_polar(self, iviewer): axis = self.figure.add_subplot(grid[3, 0], sharex=self.axis) data = (tth, self.compute_azimuthal_integral_sum()) unscaled = (tth, self.compute_azimuthal_integral_sum(False)) - self.azimuthal_line_artist, = axis.plot(*data) + self.azimuthal_line_artist, = axis.plot(*data, '-k', lw=2.5) HexrdConfig().last_unscaled_azimuthal_integral_data = unscaled self.azimuthal_integral_axis = axis - axis.set_ylabel(r'Azimuthal Average') + axis.set_ylabel(r'Azimuthal Average', fontsize=14, family='serif') self.update_azimuthal_plot_overlays() self.update_wppf_plot() @@ -1067,15 +1094,37 @@ def finish_show_polar(self, iviewer): formatter = PolarXAxisFormatter(default_formatter, f) axis.xaxis.set_major_formatter(formatter) + axis.yaxis.set_major_locator(AutoLocator()) + axis.yaxis.set_minor_locator(AutoMinorLocator()) + + axis.xaxis.set_major_locator(AutoLocator()) + axis.xaxis.set_minor_locator(AutoMinorLocator()) + + kwargs = { + 'left': True, + 'right': True, + 'bottom': True, + 'top': True, + 'which': 'major', + 'length': 10, + 'labelfontfamily': 'serif', + 'labelsize': 14 + } + axis.tick_params(**kwargs) + + kwargs['which'] = 'minor' + kwargs['length'] = 2 + axis.tick_params(**kwargs) + # Set our custom tick locators as well - self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) + # self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) + # axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) else: self.update_azimuthal_integral_plot() axis = self.azimuthal_integral_axis # Update the xlabel in case it was modified (via tth distortion) - axis.set_xlabel(self.polar_xlabel) + axis.set_xlabel(self.polar_xlabel, fontsize=14, family='serif') else: if len(self.axes_images) == 0: self.axis = self.figure.add_subplot(111) @@ -1087,13 +1136,13 @@ def finish_show_polar(self, iviewer): 'interpolation': 'none', } self.axes_images.append(self.axis.imshow(**kwargs)) - self.axis.set_ylabel(r'$\eta$ [deg]') + self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=14, family='serif') else: rescale_image = False self.axes_images[0].set_data(img) # Update the xlabel in case it was modified (via tth distortion) - self.axis.set_xlabel(self.polar_xlabel) + self.axis.set_xlabel(self.polar_xlabel, fontsize=14, family='serif') if rescale_image: self.axis.relim() @@ -1193,7 +1242,8 @@ def polar_x_axis_type(self): def on_polar_x_axis_type_changed(self): # Update the x-label - self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel) + self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, + fontsize=14, family='serif') # Still need to draw if the x-label was modified self.draw_idle() From 320253413f38a184a2f1a1e7064b86cf53396907 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Mon, 4 Nov 2024 14:43:14 -0800 Subject: [PATCH 2/9] change size from 14 to 15 --- hexrdgui/image_canvas.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 746c924bc..3954f5cd2 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1064,7 +1064,7 @@ def finish_show_polar(self, iviewer): # Do not allow the axis to autoscale, which could happen if # overlays are drawn out-of-bounds self.axis.autoscale(False) - self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=14, family='serif') + self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=15, family='serif') self.axis.label_outer() else: rescale_image = False @@ -1084,7 +1084,7 @@ def finish_show_polar(self, iviewer): HexrdConfig().last_unscaled_azimuthal_integral_data = unscaled self.azimuthal_integral_axis = axis - axis.set_ylabel(r'Azimuthal Average', fontsize=14, family='serif') + axis.set_ylabel(r'Azimuthal Average', fontsize=15, family='serif') self.update_azimuthal_plot_overlays() self.update_wppf_plot() @@ -1124,7 +1124,7 @@ def finish_show_polar(self, iviewer): axis = self.azimuthal_integral_axis # Update the xlabel in case it was modified (via tth distortion) - axis.set_xlabel(self.polar_xlabel, fontsize=14, family='serif') + axis.set_xlabel(self.polar_xlabel, fontsize=15, family='serif') else: if len(self.axes_images) == 0: self.axis = self.figure.add_subplot(111) @@ -1136,13 +1136,13 @@ def finish_show_polar(self, iviewer): 'interpolation': 'none', } self.axes_images.append(self.axis.imshow(**kwargs)) - self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=14, family='serif') + self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=15, family='serif') else: rescale_image = False self.axes_images[0].set_data(img) # Update the xlabel in case it was modified (via tth distortion) - self.axis.set_xlabel(self.polar_xlabel, fontsize=14, family='serif') + self.axis.set_xlabel(self.polar_xlabel, fontsize=15, family='serif') if rescale_image: self.axis.relim() @@ -1243,7 +1243,7 @@ def polar_x_axis_type(self): def on_polar_x_axis_type_changed(self): # Update the x-label self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, - fontsize=14, family='serif') + fontsize=15, family='serif') # Still need to draw if the x-label was modified self.draw_idle() From 92300256be7c4a599ebf6b3e0c49743a4298c0b5 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Mon, 4 Nov 2024 14:44:18 -0800 Subject: [PATCH 3/9] PEP8. --- hexrdgui/image_canvas.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 3954f5cd2..8158e4c65 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1058,13 +1058,16 @@ def finish_show_polar(self, iviewer): kwargs['length'] = 2 self.axis.tick_params(**kwargs) - self.axis.tick_params(bottom=True, top=True, which='major', length=8) - self.axis.tick_params(bottom=True, top=True, which='minor', length=2) + self.axis.tick_params( + bottom=True, top=True, which='major', length=8) + self.axis.tick_params( + bottom=True, top=True, which='minor', length=2) # Do not allow the axis to autoscale, which could happen if # overlays are drawn out-of-bounds self.axis.autoscale(False) - self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=15, family='serif') + self.axis.set_ylabel( + r'$\phi$ [deg]', fontsize=15, family='serif') self.axis.label_outer() else: rescale_image = False @@ -1144,6 +1147,7 @@ def finish_show_polar(self, iviewer): # Update the xlabel in case it was modified (via tth distortion) self.axis.set_xlabel(self.polar_xlabel, fontsize=15, family='serif') + if rescale_image: self.axis.relim() self.axis.autoscale_view() @@ -1237,13 +1241,14 @@ def on_beam_energy_modified(self): self.iviewer.instr.beam_energy = HexrdConfig().beam_energy @property - def polar_x_axis_type(self): + def polar_x_axis_type(self): return HexrdConfig().polar_x_axis_type def on_polar_x_axis_type_changed(self): # Update the x-label self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, - fontsize=15, family='serif') + fontsize=15, family='serif') + # Still need to draw if the x-label was modified self.draw_idle() From 849a778c078e97c9cdb59f9b36721ce818c06225 Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Mon, 4 Nov 2024 15:00:07 -0800 Subject: [PATCH 4/9] fix indent. --- hexrdgui/image_canvas.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 8158e4c65..9b011b472 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1241,14 +1241,13 @@ def on_beam_energy_modified(self): self.iviewer.instr.beam_energy = HexrdConfig().beam_energy @property - def polar_x_axis_type(self): + def polar_x_axis_type(self): return HexrdConfig().polar_x_axis_type def on_polar_x_axis_type_changed(self): # Update the x-label self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, - fontsize=15, family='serif') - + fontsize=15, family='serif') # Still need to draw if the x-label was modified self.draw_idle() From d85832034e8b697601d42bb973fcaa7a61fd328b Mon Sep 17 00:00:00 2001 From: Saransh Singh Date: Mon, 4 Nov 2024 15:28:39 -0800 Subject: [PATCH 5/9] add grid to azimuthal average --- hexrdgui/image_canvas.py | 47 +++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 8 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 9b011b472..a89fda005 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -35,6 +35,8 @@ ) from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed +FONTSIZE_LABEL=15 +FONTSIZE_TICKS=15 class ImageCanvas(FigureCanvas): @@ -1050,7 +1052,7 @@ def finish_show_polar(self, iviewer): 'which': 'major', 'length': 10, 'labelfontfamily': 'serif', - 'labelsize': 14 + 'labelsize': FONTSIZE_TICKS, } self.axis.tick_params(**kwargs) @@ -1067,7 +1069,7 @@ def finish_show_polar(self, iviewer): # overlays are drawn out-of-bounds self.axis.autoscale(False) self.axis.set_ylabel( - r'$\phi$ [deg]', fontsize=15, family='serif') + r'$\phi$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') self.axis.label_outer() else: rescale_image = False @@ -1087,7 +1089,7 @@ def finish_show_polar(self, iviewer): HexrdConfig().last_unscaled_azimuthal_integral_data = unscaled self.azimuthal_integral_axis = axis - axis.set_ylabel(r'Azimuthal Average', fontsize=15, family='serif') + axis.set_ylabel(r'Azimuthal Average', fontsize=FONTSIZE_LABEL, family='serif') self.update_azimuthal_plot_overlays() self.update_wppf_plot() @@ -1103,6 +1105,7 @@ def finish_show_polar(self, iviewer): axis.xaxis.set_major_locator(AutoLocator()) axis.xaxis.set_minor_locator(AutoMinorLocator()) + # change property of ticks kwargs = { 'left': True, 'right': True, @@ -1111,7 +1114,7 @@ def finish_show_polar(self, iviewer): 'which': 'major', 'length': 10, 'labelfontfamily': 'serif', - 'labelsize': 14 + 'labelsize': FONTSIZE_TICKS, } axis.tick_params(**kwargs) @@ -1119,6 +1122,34 @@ def finish_show_polar(self, iviewer): kwargs['length'] = 2 axis.tick_params(**kwargs) + # add grid lines parallel to x-axis in azimuthal average + kwargs = { + 'visible': True, + 'which': 'major', + 'axis': 'y', + 'linewidth': 0.25, + 'linestyle': '-', + 'color': 'k', + 'alpha': 0.75, + } + axis.grid(**kwargs) + + kwargs = { + 'visible': True, + 'which': 'minor', + 'axis': 'y', + 'linewidth': 0.075, + 'linestyle': '--', + 'color': 'k', + 'alpha': 0.9, + } + axis.grid(**kwargs) + + # add grid lines parallel to y-axis + kwargs['which'] = 'both' + kwargs['axis'] = 'x' + axis.grid(**kwargs) + # Set our custom tick locators as well # self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) # axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) @@ -1127,7 +1158,7 @@ def finish_show_polar(self, iviewer): axis = self.azimuthal_integral_axis # Update the xlabel in case it was modified (via tth distortion) - axis.set_xlabel(self.polar_xlabel, fontsize=15, family='serif') + axis.set_xlabel(self.polar_xlabel, fontsize=FONTSIZE_LABEL, family='serif') else: if len(self.axes_images) == 0: self.axis = self.figure.add_subplot(111) @@ -1139,13 +1170,13 @@ def finish_show_polar(self, iviewer): 'interpolation': 'none', } self.axes_images.append(self.axis.imshow(**kwargs)) - self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=15, family='serif') + self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') else: rescale_image = False self.axes_images[0].set_data(img) # Update the xlabel in case it was modified (via tth distortion) - self.axis.set_xlabel(self.polar_xlabel, fontsize=15, family='serif') + self.axis.set_xlabel(self.polar_xlabel, fontsize=FONTSIZE_LABEL, family='serif') if rescale_image: @@ -1247,7 +1278,7 @@ def polar_x_axis_type(self): def on_polar_x_axis_type_changed(self): # Update the x-label self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, - fontsize=15, family='serif') + fontsize=FONTSIZE_LABEL, family='serif') # Still need to draw if the x-label was modified self.draw_idle() From c4e98a59b1ced5e00cd86bc750b2aa87cf7f8ed3 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Wed, 13 Nov 2024 12:30:39 -0600 Subject: [PATCH 6/9] Use our custom x axis locator for major ticks This makes ticks at Q values of 1, 2, 3, etc. Signed-off-by: Patrick Avery --- hexrdgui/image_canvas.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index a89fda005..df59b675c 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1041,7 +1041,7 @@ def finish_show_polar(self, iviewer): self.axis.yaxis.set_major_locator(AutoLocator()) self.axis.yaxis.set_minor_locator(AutoMinorLocator()) - self.axis.xaxis.set_major_locator(AutoLocator()) + self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) self.axis.xaxis.set_minor_locator(AutoMinorLocator()) kwargs = { @@ -1102,7 +1102,7 @@ def finish_show_polar(self, iviewer): axis.yaxis.set_major_locator(AutoLocator()) axis.yaxis.set_minor_locator(AutoMinorLocator()) - axis.xaxis.set_major_locator(AutoLocator()) + axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) axis.xaxis.set_minor_locator(AutoMinorLocator()) # change property of ticks @@ -1149,10 +1149,6 @@ def finish_show_polar(self, iviewer): kwargs['which'] = 'both' kwargs['axis'] = 'x' axis.grid(**kwargs) - - # Set our custom tick locators as well - # self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - # axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) else: self.update_azimuthal_integral_plot() axis = self.azimuthal_integral_axis From f18a978c7fe3eccc4f366812f739dd1342e11513 Mon Sep 17 00:00:00 2001 From: Christos Tsolakis Date: Fri, 15 Nov 2024 16:14:57 -0500 Subject: [PATCH 7/9] Add custom minor tick class for polar plots --- hexrdgui/image_canvas.py | 61 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index df59b675c..1ca81958f 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1,5 +1,6 @@ import copy import math +import logging from PySide6.QtCore import QThreadPool, QTimer, Signal, Qt from PySide6.QtWidgets import QFileDialog, QMessageBox @@ -10,6 +11,7 @@ from matplotlib.patches import Circle from matplotlib.ticker import AutoLocator, AutoMinorLocator, FuncFormatter +import matplotlib as mpl import matplotlib.pyplot as plt import matplotlib.transforms as tx @@ -1042,7 +1044,7 @@ def finish_show_polar(self, iviewer): self.axis.yaxis.set_minor_locator(AutoMinorLocator()) self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - self.axis.xaxis.set_minor_locator(AutoMinorLocator()) + self.axis.xaxis.set_minor_locator(PolarXAxisMinorTickLocator(self)) kwargs = { 'left': True, @@ -1103,7 +1105,7 @@ def finish_show_polar(self, iviewer): axis.yaxis.set_minor_locator(AutoMinorLocator()) axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - axis.xaxis.set_minor_locator(AutoMinorLocator()) + self.axis.xaxis.set_minor_locator(PolarXAxisMinorTickLocator(self)) # change property of ticks kwargs = { @@ -1836,6 +1838,61 @@ def tick_values(self, vmin, vmax): # Convert back to tth return canvas.polar_x_type_to_tth(values) +class PolarXAxisMinorTickLocator(AutoMinorLocator): + """Subclass the tick locator so we can modify its behavior + + We will modify any value ranges provided so that the current x-axis type + provides nice looking ticks. + + For instance, for Q, we want to space minor ticks non-linearly between major ticks + """ + def __init__(self, canvas, *args, **kwargs): + super().__init__(*args, **kwargs) + self._hexrdgui_canvas = canvas + + def __call__(self): + canvas = self._hexrdgui_canvas + if self.axis.get_scale() == 'log': + logging.warning('PolarXAxisMinorTickLocator does not work on logarithmic scales') + return [] + + majorlocs = np.unique(self.axis.get_majorticklocs()) + if len(majorlocs) < 2: + # Need at least two major ticks to find minor tick locations. + # TODO: Figure out a way to still be able to display minor ticks with less + # than two major ticks visible. For now, just display no ticks at all. + return [] + + # Convert to our current x type + majorlocs = canvas.polar_tth_to_x_type(majorlocs) + majorstep = majorlocs[1] - majorlocs[0] + + if self.ndivs is None: + self.ndivs = mpl.rcParams[ + 'ytick.minor.ndivs' if self.axis.axis_name == 'y' + else 'xtick.minor.ndivs'] # for x and z axis + + if self.ndivs == 'auto': + majorstep_mantissa = 10 ** (np.log10(majorstep) % 1) + ndivs = 5 if np.isclose(majorstep_mantissa, [1, 2.5, 5, 10]).any() else 4 + else: + ndivs = self.ndivs + + minorstep = majorstep / ndivs + + vmin, vmax = sorted(self.axis.get_view_interval()) + # Convert to our current x type + vmin, vmax = canvas.polar_tth_to_x_type([vmin, vmax]) + t0 = majorlocs[0] + tmin = round((vmin - t0) / minorstep) + tmax = round((vmax - t0) / minorstep) + 1 + locs = (np.arange(tmin, tmax) * minorstep) + t0 + + # Convert back to tth + locs = canvas.polar_x_type_to_tth(locs) + + return self.raise_if_exceeds(locs) + class PolarXAxisFormatter(FuncFormatter): """Subclass the func formatter so we can keep the default formatter in sync From b4ecc6cb940aea52a731b5c884300a50a5bae721 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Sat, 23 Nov 2024 19:24:54 -0600 Subject: [PATCH 8/9] Change phi back to eta for now This might need to be an editable option in the future. Signed-off-by: Patrick Avery --- hexrdgui/image_canvas.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index 1ca81958f..a3ad459b1 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -1071,7 +1071,7 @@ def finish_show_polar(self, iviewer): # overlays are drawn out-of-bounds self.axis.autoscale(False) self.axis.set_ylabel( - r'$\phi$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') + r'$\eta$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') self.axis.label_outer() else: rescale_image = False @@ -1168,7 +1168,7 @@ def finish_show_polar(self, iviewer): 'interpolation': 'none', } self.axes_images.append(self.axis.imshow(**kwargs)) - self.axis.set_ylabel(r'$\phi$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') + self.axis.set_ylabel(r'$\eta$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') else: rescale_image = False self.axes_images[0].set_data(img) From 555682cb3da268b5e0cac352ed62219ffa8d76e9 Mon Sep 17 00:00:00 2001 From: Patrick Avery Date: Sat, 23 Nov 2024 19:54:36 -0600 Subject: [PATCH 9/9] Use relative increase for font sizes This way, we can still increase their font sizes if the global font size increases. Signed-off-by: Patrick Avery --- hexrdgui/image_canvas.py | 130 ++++++++++++++++++++++----------------- 1 file changed, 75 insertions(+), 55 deletions(-) diff --git a/hexrdgui/image_canvas.py b/hexrdgui/image_canvas.py index a3ad459b1..fa982174c 100644 --- a/hexrdgui/image_canvas.py +++ b/hexrdgui/image_canvas.py @@ -37,8 +37,11 @@ ) from hexrdgui.utils.tth_distortion import apply_tth_distortion_if_needed -FONTSIZE_LABEL=15 -FONTSIZE_TICKS=15 +# Increase these font sizes (compared to the global font) by the specified +# amounts. +FONTSIZE_LABEL_INCREASE = 4 +FONTSIZE_TICKS_INCREASE = 4 + class ImageCanvas(FigureCanvas): @@ -118,6 +121,42 @@ def setup_connections(self): def thread_pool(self): return QThreadPool.globalInstance() + @property + def fontsize_label(self): + return HexrdConfig().font_size + FONTSIZE_LABEL_INCREASE + + @property + def fontsize_ticks(self): + return HexrdConfig().font_size + FONTSIZE_TICKS_INCREASE + + @property + def label_kwargs(self): + return { + 'fontsize': self.fontsize_label, + 'family': 'serif', + } + + @property + def major_tick_kwargs(self): + return { + 'left': True, + 'right': True, + 'bottom': True, + 'top': True, + 'which': 'major', + 'length': 10, + 'labelfontfamily': 'serif', + 'labelsize': self.fontsize_ticks, + } + + @property + def minor_tick_kwargs(self): + return { + **self.major_tick_kwargs, + 'which': 'minor', + 'length': 2, + } + def __del__(self): # This is so that the figure can be cleaned up plt.close(self.figure) @@ -1044,34 +1083,16 @@ def finish_show_polar(self, iviewer): self.axis.yaxis.set_minor_locator(AutoMinorLocator()) self.axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - self.axis.xaxis.set_minor_locator(PolarXAxisMinorTickLocator(self)) - - kwargs = { - 'left': True, - 'right': True, - 'bottom': True, - 'top': True, - 'which': 'major', - 'length': 10, - 'labelfontfamily': 'serif', - 'labelsize': FONTSIZE_TICKS, - } - self.axis.tick_params(**kwargs) - - kwargs['which'] = 'minor' - kwargs['length'] = 2 - self.axis.tick_params(**kwargs) - - self.axis.tick_params( - bottom=True, top=True, which='major', length=8) - self.axis.tick_params( - bottom=True, top=True, which='minor', length=2) + self.axis.xaxis.set_minor_locator( + PolarXAxisMinorTickLocator(self) + ) + self.axis.tick_params(**self.major_tick_kwargs) + self.axis.tick_params(**self.minor_tick_kwargs) # Do not allow the axis to autoscale, which could happen if # overlays are drawn out-of-bounds self.axis.autoscale(False) - self.axis.set_ylabel( - r'$\eta$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') + self.axis.set_ylabel(r'$\eta$ [deg]', **self.label_kwargs) self.axis.label_outer() else: rescale_image = False @@ -1091,7 +1112,7 @@ def finish_show_polar(self, iviewer): HexrdConfig().last_unscaled_azimuthal_integral_data = unscaled self.azimuthal_integral_axis = axis - axis.set_ylabel(r'Azimuthal Average', fontsize=FONTSIZE_LABEL, family='serif') + axis.set_ylabel(r'Azimuthal Average', **self.label_kwargs) self.update_azimuthal_plot_overlays() self.update_wppf_plot() @@ -1105,24 +1126,13 @@ def finish_show_polar(self, iviewer): axis.yaxis.set_minor_locator(AutoMinorLocator()) axis.xaxis.set_major_locator(PolarXAxisTickLocator(self)) - self.axis.xaxis.set_minor_locator(PolarXAxisMinorTickLocator(self)) + self.axis.xaxis.set_minor_locator( + PolarXAxisMinorTickLocator(self) + ) # change property of ticks - kwargs = { - 'left': True, - 'right': True, - 'bottom': True, - 'top': True, - 'which': 'major', - 'length': 10, - 'labelfontfamily': 'serif', - 'labelsize': FONTSIZE_TICKS, - } - axis.tick_params(**kwargs) - - kwargs['which'] = 'minor' - kwargs['length'] = 2 - axis.tick_params(**kwargs) + axis.tick_params(**self.major_tick_kwargs) + axis.tick_params(**self.minor_tick_kwargs) # add grid lines parallel to x-axis in azimuthal average kwargs = { @@ -1156,7 +1166,7 @@ def finish_show_polar(self, iviewer): axis = self.azimuthal_integral_axis # Update the xlabel in case it was modified (via tth distortion) - axis.set_xlabel(self.polar_xlabel, fontsize=FONTSIZE_LABEL, family='serif') + axis.set_xlabel(self.polar_xlabel, **self.label_kwargs) else: if len(self.axes_images) == 0: self.axis = self.figure.add_subplot(111) @@ -1168,14 +1178,13 @@ def finish_show_polar(self, iviewer): 'interpolation': 'none', } self.axes_images.append(self.axis.imshow(**kwargs)) - self.axis.set_ylabel(r'$\eta$ [deg]', fontsize=FONTSIZE_LABEL, family='serif') + self.axis.set_ylabel(r'$\eta$ [deg]', **self.label_kwargs) else: rescale_image = False self.axes_images[0].set_data(img) # Update the xlabel in case it was modified (via tth distortion) - self.axis.set_xlabel(self.polar_xlabel, fontsize=FONTSIZE_LABEL, family='serif') - + self.axis.set_xlabel(self.polar_xlabel, **self.label_kwargs) if rescale_image: self.axis.relim() @@ -1275,8 +1284,8 @@ def polar_x_axis_type(self): def on_polar_x_axis_type_changed(self): # Update the x-label - self.azimuthal_integral_axis.set_xlabel(self.polar_xlabel, - fontsize=FONTSIZE_LABEL, family='serif') + self.azimuthal_integral_axis.set_xlabel( + self.polar_xlabel, **self.label_kwargs) # Still need to draw if the x-label was modified self.draw_idle() @@ -1784,7 +1793,12 @@ def draw_mask_boundaries(self, axis, det=None): delta_eta_est = np.nanmedian(eta_diff) tolerance = delta_eta_est * 10 big_gaps, = np.nonzero(eta_diff > tolerance) - verts[i] = np.insert(vert, big_gaps + 1, np.nan, axis=0) + verts[i] = np.insert( + vert, + big_gaps + 1, + np.nan, + axis=0, + ) if self.mode == ViewType.stereo: # Now convert from polar to stereo @@ -1838,13 +1852,15 @@ def tick_values(self, vmin, vmax): # Convert back to tth return canvas.polar_x_type_to_tth(values) + class PolarXAxisMinorTickLocator(AutoMinorLocator): """Subclass the tick locator so we can modify its behavior We will modify any value ranges provided so that the current x-axis type provides nice looking ticks. - For instance, for Q, we want to space minor ticks non-linearly between major ticks + For instance, for Q, we want to space minor ticks non-linearly between + major ticks """ def __init__(self, canvas, *args, **kwargs): super().__init__(*args, **kwargs) @@ -1853,14 +1869,15 @@ def __init__(self, canvas, *args, **kwargs): def __call__(self): canvas = self._hexrdgui_canvas if self.axis.get_scale() == 'log': - logging.warning('PolarXAxisMinorTickLocator does not work on logarithmic scales') + logging.warning( + 'PolarXAxisMinorTickLocator does not work on logarithmic ' + 'scales' + ) return [] majorlocs = np.unique(self.axis.get_majorticklocs()) if len(majorlocs) < 2: # Need at least two major ticks to find minor tick locations. - # TODO: Figure out a way to still be able to display minor ticks with less - # than two major ticks visible. For now, just display no ticks at all. return [] # Convert to our current x type @@ -1874,7 +1891,10 @@ def __call__(self): if self.ndivs == 'auto': majorstep_mantissa = 10 ** (np.log10(majorstep) % 1) - ndivs = 5 if np.isclose(majorstep_mantissa, [1, 2.5, 5, 10]).any() else 4 + if np.isclose(majorstep_mantissa, [1, 2.5, 5, 10]).any(): + ndivs = 5 + else: + ndivs = 4 else: ndivs = self.ndivs