Skip to content

Commit 3b1587a

Browse files
committed
Adjust histogram boundaries when having constant distribution: min==max
- Add test for plot histogram when having a constant distribution
1 parent ebc4d03 commit 3b1587a

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

src/ert/gui/tools/plot/plottery/plots/histogram.py

+3
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,9 @@ def plotHistogram(
133133
),
134134
)
135135
else:
136+
if minimum is not None and maximum is not None and minimum == maximum:
137+
minimum -= 0.1
138+
maximum += 0.1
136139
config.addLegendItem(
137140
ensemble.name,
138141
_plotHistogram(

tests/ert/unit_tests/gui/plottery/test_histogram.py

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
from unittest.mock import Mock
1+
from unittest.mock import ANY, Mock
22

33
import pandas as pd
44
import pytest
55
from matplotlib.figure import Figure
66

7+
import ert
78
from ert.gui.tools.plot.plot_api import EnsembleObject
89
from ert.gui.tools.plot.plottery import PlotConfig, PlotContext
910
from ert.gui.tools.plot.plottery.plots import HistogramPlot
@@ -70,3 +71,42 @@ def test_histogram(plot_context: PlotContext, ensemble_to_data_map):
7071
{},
7172
)
7273
return figure
74+
75+
76+
def test_histogram_plot_for_constant_distribution(monkeypatch):
77+
# test that the histogram plot is called with the correct min and max values
78+
# when all the parameter values are the same
79+
context = Mock(spec=PlotContext)
80+
context.log_scale = False
81+
context.ensembles.return_value = [
82+
EnsembleObject("ensemble_1", "id", False, "experiment_1")
83+
]
84+
title = "Histogram with same values"
85+
context.plotConfig.return_value = PlotConfig(title=title)
86+
value = 0
87+
data_map = dict.fromkeys(context.ensembles(), pd.DataFrame([10 * [value]]))
88+
min_value = value - 0.1
89+
max_value = value + 0.1
90+
figure = Figure()
91+
mock_plot_histogram = Mock()
92+
monkeypatch.setattr(
93+
ert.gui.tools.plot.plottery.plots.histogram,
94+
"_plotHistogram",
95+
mock_plot_histogram,
96+
)
97+
HistogramPlot().plot(
98+
figure,
99+
context,
100+
data_map,
101+
pd.DataFrame(),
102+
{},
103+
)
104+
mock_plot_histogram.assert_called_once_with(
105+
ANY,
106+
ANY,
107+
ANY,
108+
ANY,
109+
ANY,
110+
min_value,
111+
max_value,
112+
)

0 commit comments

Comments
 (0)