Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 57 additions & 1 deletion rascal2/widgets/plot.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
"""The Plot MDI widget."""

import copy
from abc import abstractmethod
from inspect import isclass

import matplotlib
import ratapi
from cycler import cycler
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg, NavigationToolbar2QT
from PyQt6 import QtCore, QtGui, QtWidgets

Expand Down Expand Up @@ -388,7 +390,61 @@ def export(self):
if accepted:
sx = self.figure.get_figwidth() * self.figure.dpi
dpi = self.figure.dpi if sx > 1920 else 1920 // self.figure.get_figwidth()
self.figure.savefig(filepath, facecolor=SETTINGS.export_background_colour, dpi=dpi)
scheme = get_correct_qt_color_scheme()
if scheme == QtCore.Qt.ColorScheme.Light:
self.figure.savefig(filepath, facecolor=SETTINGS.export_background_colour, dpi=dpi)
else:
old_colours = matplotlib.rcParams["axes.prop_cycle"].by_key()["color"]

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest a refactor to get rid of hardcoded colours, let me know if it works

old_colours = matplotlib.rcParams["axes.prop_cycle"].by_key()["color"]
with matplotlib.style.context("default"):
    edge_colour = matplotlib.rcParams['axes.edgecolor']
    face_colour = matplotlib.rcParams['axes.facecolor']
    new_colours = matplotlib.rcParams["axes.prop_cycle"].by_key()["color"]
    colour_converter = dict(zip(old_colours, new_colours, strict=False))
    temp_fig = copy.deepcopy(self.figure)
    axes = temp_fig.axes
    for ax in axes:
        if not ax.get_visible():
            continue
        if ax.containers:
            for container in ax.containers:
                if isinstance(container, matplotlib.container.ErrorbarContainer):
                    _, __, (vertical_lines,) = container.lines
                    vertical_lines.set_color(
                        colour_converter[
                            matplotlib.colors.rgb2hex(vertical_lines.get_color(), keep_alpha=False)
                        ]
                    )
        ax.patch.set_facecolor(face_colour)
        for spine in ax.spines.values():
            spine.set_edgecolor(edge_colour)
        ax.tick_params(which="both", axis="both", color="black", labelcolor=edge_colour)
        ax.xaxis.label.set_color(edge_colour)
        ax.yaxis.label.set_color(edge_colour)
        ax.set_title(ax.get_title(loc="left"), color=edge_colour, loc="left")
        if ax.get_legend() is not None:
            ax.legend(facecolor="white", labelcolor=edge_colour)
            for line in ax.get_legend().get_lines():
                old_line_colour = line.get_color()
                line.set_color(colour_converter[old_line_colour])
        for line in ax.get_lines():
            old_line_colour = line.get_color()
            line.set_color(colour_converter[old_line_colour])

    temp_fig.savefig(filepath, facecolor=SETTINGS.export_background_colour, dpi=dpi)

new_colours_cycle = cycler(
color=[
"#1f77b4",
"#ff7f0e",
"#2ca02c",
"#d62728",
"#9467bd",
"#8c564b",
"#e377c2",
"#7f7f7f",
"#bcbd22",
"#17becf",
]
)
new_colours = new_colours_cycle.by_key()["color"]
colour_converter = dict(zip(old_colours, new_colours, strict=False))
temp_fig = copy.deepcopy(self.figure)
axes = temp_fig.axes
for ax in axes:
if ax.containers:
for container in ax.containers:
if isinstance(container, matplotlib.container.ErrorbarContainer):
_, __, (vertical_lines,) = container.lines
vertical_lines.set_color(
colour_converter[
matplotlib.colors.rgb2hex(vertical_lines.get_color(), keep_alpha=False)
]
)
ax.set_prop_cycle(new_colours_cycle)
ax.patch.set_facecolor("white")
ax.spines["bottom"].set_color("black")
ax.spines["top"].set_color("black")
ax.spines["right"].set_color("black")
ax.spines["left"].set_color("black")
ax.tick_params(which="both", axis="both", color="black", labelcolor="black")
ax.xaxis.label.set_color("black")
ax.yaxis.label.set_color("black")
title_text = ax.get_title(loc="left")
ax.set_title(title_text, color="black", loc="left")
ax.title.set_color("black")
if ax.get_legend() is not None:
ax.legend(facecolor="white", labelcolor="black")
for line in ax.get_legend().get_lines():
old_line_colour = line.get_color()
line.set_color(colour_converter[old_line_colour])
for line in ax.get_lines():
old_line_colour = line.get_color()
line.set_color(colour_converter[old_line_colour])

temp_fig.savefig(filepath, facecolor=SETTINGS.export_background_colour, dpi=dpi)

def changeEvent(self, event):
if self.toolbar is not None and event.type() == QtCore.QEvent.Type.PaletteChange:
Expand Down
Loading