Skip to content

Commit fc5f535

Browse files
Apply formatting to stress-strain plots
1 parent 7b57033 commit fc5f535

File tree

3 files changed

+52
-11
lines changed

3 files changed

+52
-11
lines changed

docs/examples/moment_curvature.ipynb

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
" SteelBar,\n",
3434
" add_bar_rectangular_array,\n",
3535
")\n",
36-
"from concreteproperties.post import si_kn_m\n",
36+
"from concreteproperties.post import si_kn_m, si_n_mm\n",
3737
"from concreteproperties.results import MomentCurvatureResults"
3838
]
3939
},
@@ -141,7 +141,9 @@
141141
"outputs": [],
142142
"source": [
143143
"for conc in conc_material_list:\n",
144-
" conc.stress_strain_profile.plot_stress_strain(title=conc.name)"
144+
" conc.stress_strain_profile.plot_stress_strain(\n",
145+
" title=conc.name, eng=True, units=si_n_mm\n",
146+
" )"
145147
]
146148
},
147149
{
@@ -151,7 +153,9 @@
151153
"metadata": {},
152154
"outputs": [],
153155
"source": [
154-
"steel.stress_strain_profile.plot_stress_strain(title=steel.name)"
156+
"steel.stress_strain_profile.plot_stress_strain(\n",
157+
" title=steel.name, eng=True, units=si_n_mm\n",
158+
")"
155159
]
156160
},
157161
{

docs/examples/prestressed_section.ipynb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,10 @@
141141
"outputs": [],
142142
"source": [
143143
"# combine both stress-strain profiles on the same plot\n",
144-
"ax = concrete_service.stress_strain_profile.plot_stress_strain(render=False)\n",
145-
"concrete_service.ultimate_stress_strain_profile.plot_stress_strain(ax=ax)\n",
144+
"ax = concrete_service.stress_strain_profile.plot_stress_strain(\n",
145+
" units=si_n_mm, render=False\n",
146+
")\n",
147+
"concrete_service.ultimate_stress_strain_profile.plot_stress_strain(units=si_n_mm, ax=ax)\n",
146148
"ax.lines[0].set_label(\"Service\")\n",
147149
"ax.lines[1].set_label(\"Ultimate\")\n",
148150
"ax.legend(loc=\"center left\", bbox_to_anchor=(1, 0.5))\n",
@@ -158,8 +160,10 @@
158160
"outputs": [],
159161
"source": [
160162
"# combine both stress-strain profiles on the same plot\n",
161-
"ax = strand_service.stress_strain_profile.plot_stress_strain(render=False)\n",
162-
"strand_ultimate.stress_strain_profile.plot_stress_strain(ax=ax)\n",
163+
"ax = strand_service.stress_strain_profile.plot_stress_strain(\n",
164+
" eng=True, units=si_kn_m, render=False\n",
165+
")\n",
166+
"strand_ultimate.stress_strain_profile.plot_stress_strain(eng=True, units=si_kn_m, ax=ax)\n",
163167
"ax.lines[0].set_label(\"Service\")\n",
164168
"ax.lines[1].set_label(\"Ultimate\")\n",
165169
"ax.legend(loc=\"center left\", bbox_to_anchor=(1, 0.5))\n",

src/concreteproperties/stress_strain_profile.py

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,17 @@
88

99
import matplotlib.pyplot as plt
1010
import numpy as np
11+
from matplotlib.ticker import FuncFormatter
1112
from rich.console import Console
1213
from rich.table import Table
1314
from scipy.interpolate import interp1d
1415
from scipy.optimize import brentq
1516

16-
from concreteproperties.post import plotting_context
17+
from concreteproperties.post import (
18+
UnitDisplay,
19+
plotting_context,
20+
string_formatter_plots,
21+
)
1722

1823
if TYPE_CHECKING:
1924
import matplotlib.axes
@@ -214,27 +219,55 @@ def plot_stress_strain(
214219
self,
215220
title: str = "Stress-Strain Profile",
216221
fmt: str = "o-",
222+
eng: bool = False,
223+
prec: int = 2,
224+
units: UnitDisplay | None = None,
217225
**kwargs,
218226
) -> matplotlib.axes.Axes:
219227
"""Plots the stress-strain profile.
220228
221229
Args:
222230
title: Plot title. Defaults to ``"Stress-Strain Profile"``.
223231
fmt: Plot format string. Defaults to ``"o-"``.
232+
eng: If set to ``True``, formats the plot ticks with engineering notation.
233+
If set to ``False``, uses the default ``matplotlib`` ticker formatting.
234+
Defaults to ``False``.
235+
prec: If ``eng=True``, sets the desired precision of the ticker formatting
236+
(i.e. one plus this value is the desired number of digits). Defaults to
237+
``2``.
238+
units: Unit system to display. Defaults to ``None``.
224239
kwargs: Passed to :func:`~concreteproperties.post.plotting_context`
225240
226241
Returns:
227242
Matplotlib axes object
228243
"""
244+
# assign default unit if no units applied
245+
if units is None:
246+
units = UnitDisplay("", "", "")
247+
stress_unit = "-"
248+
else:
249+
stress_unit = units.stress_unit[1:]
250+
229251
# create plot and setup the plot
230252
with plotting_context(title=title, **kwargs) as (_, ax):
231253
if ax is None:
232254
msg = "Plot failed."
233255
raise RuntimeError(msg)
234256

235-
ax.plot(self.strains, self.stresses, fmt)
236-
plt.xlabel("Strain")
237-
plt.ylabel("Stress")
257+
# scale stresses
258+
stresses = np.array(self.stresses) * units.stress_scale
259+
260+
ax.plot(self.strains, stresses, fmt)
261+
262+
if eng:
263+
tick_formatter = FuncFormatter(
264+
lambda x, _: string_formatter_plots(value=x, prec=prec)
265+
)
266+
ax.xaxis.set_major_formatter(tick_formatter)
267+
ax.yaxis.set_major_formatter(tick_formatter)
268+
269+
plt.xlabel("Strain [-]")
270+
plt.ylabel(f"Stress [{stress_unit}]")
238271
plt.grid(True)
239272

240273
return ax

0 commit comments

Comments
 (0)