Skip to content

Commit

Permalink
text wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
tbhallett committed Nov 16, 2023
1 parent 4c3cf43 commit 08b17bb
Showing 1 changed file with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
scenarios (scenario_impact_of_healthsystem.py)"""

import argparse
import textwrap
from pathlib import Path
from typing import Tuple

Expand Down Expand Up @@ -86,7 +87,7 @@ def find_difference_relative_to_comparison(_ser: pd.Series,
.drop(columns=([comparison] if drop_comparison else [])) \
.stack()

def do_bar_plot_with_ci(_df, annotations=None):
def do_bar_plot_with_ci(_df, annotations=None, xticklabels_horizontal_and_wrapped=False):
"""Make a vertical bar plot for each row of _df, using the columns to identify the height of the bar and the
extent of the error bar."""
yerr = np.array([
Expand All @@ -110,7 +111,12 @@ def do_bar_plot_with_ci(_df, annotations=None):
for xpos, ypos, text in zip(xticks.keys(), _df['upper'].values, annotations):
ax.text(xpos, ypos*1.05, text, horizontalalignment='center')
ax.set_xticks(list(xticks.keys()))
ax.set_xticklabels(list(xticks.values()), rotation=90)
if not xticklabels_horizontal_and_wrapped:
# xticklabels will be vertical and not wrapped
ax.set_xticklabels(list(xticks.values()), rotation=90)
else:
wrapped_labs = ["\n".join(textwrap.wrap(_lab, 20)) for _lab in xticks.values()]
ax.set_xticklabels(wrapped_labs)
ax.grid(axis="y")
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
Expand Down Expand Up @@ -267,9 +273,27 @@ def rename_scenarios_in_index(ser: pd.Series) -> pd.Series:
]
)
ax.set_title(name_of_plot)
ax.set_ylim(0, 14)
ax.set_yticks(np.arange(0, 16, 2))
# ax.set_xticklabels(list(ax.get_xticklabels()), rotation=0)
ax.set_ylim(0, 16)
ax.set_yticks(np.arange(0, 18, 2))
ax.set_ylabel('Additional DALYS Averted \n(Millions)')
fig.tight_layout()
fig.savefig(make_graph_file_name(name_of_plot.replace(' ', '_').replace(',', '')))
fig.show()
plt.close(fig)

# DALYS (with xtickabels horizontal and wrapped)
name_of_plot = f'Additional DALYs Averted vs Status Quo, {target_period()}'
fig, ax = do_bar_plot_with_ci(
(num_dalys_averted / 1e6).clip(lower=0.0),
annotations=[
f"{round(row['mean'], 1)} ({round(row['lower'], 1)}-{round(row['upper'], 1)}) %"
for _, row in pc_dalys_averted.clip(lower=0.0).iterrows()
],
xticklabels_horizontal_and_wrapped=True,
)
ax.set_title(name_of_plot)
ax.set_ylim(0, 16)
ax.set_yticks(np.arange(0, 18, 2))
ax.set_ylabel('Additional DALYS Averted \n(Millions)')
fig.tight_layout()
fig.savefig(make_graph_file_name(name_of_plot.replace(' ', '_').replace(',', '')))
Expand Down

0 comments on commit 08b17bb

Please sign in to comment.