Skip to content

Commit f47ec7d

Browse files
authored
Merge pull request #58 from mggg/fix-plots-for-joss
Fix plots for joss
2 parents e52d4fd + 7f314d8 commit f47ec7d

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

pyei/plot_utils.py

+13-6
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ def plot_precincts(
136136
voting_prefs_group1,
137137
voting_prefs_group2,
138138
group_names,
139+
candidate,
139140
precinct_labels=None,
140141
show_all_precincts=False,
141142
ax=None,
@@ -151,6 +152,8 @@ def plot_precincts(
151152
Same as voting_prefs_group2, except showing support among group 2
152153
group_names: list of str
153154
The demographic group names, for display in the legend
155+
candidate: str
156+
The candidate name
154157
precinct_labels : list of str (optional)
155158
The names for each precinct
156159
show_all_precincts : bool, optional
@@ -207,13 +210,13 @@ def replace_ticks_with_precinct_labels(value, pos):
207210
ax.set_yticks(np.arange(len(precinct_labels)))
208211
ax.yaxis.set_major_formatter(mticker.FuncFormatter(replace_ticks_with_precinct_labels))
209212
ax.set_title("Precinct level estimates of voting preferences", fontsize=TITLESIZE)
210-
ax.set_xlabel("Percent vote for candidate", fontsize=FONTSIZE)
213+
ax.set_xlabel(f"Percent vote for {candidate}", fontsize=FONTSIZE)
211214
ax.set_ylabel("Precinct", fontsize=FONTSIZE)
212215

213216
proxy_handles = [
214217
mpatches.Patch(color=colors[i], ec="black", label=group_names[i]) for i in range(2)
215218
]
216-
ax.legend(handles=proxy_handles, loc="upper center")
219+
ax.legend(handles=proxy_handles, prop={"size": 14}, loc="upper center")
217220
ax.set_ylim(-1, ax.get_ylim()[1])
218221
size_ticks(ax, "x")
219222
return ax
@@ -506,6 +509,7 @@ def plot_polarization_kde(
506509
color="steelblue",
507510
linewidth=0,
508511
)
512+
ax.set_ylabel("Density", fontsize=FONTSIZE)
509513
if len(thresholds) == 1:
510514
threshold_string = f"> {thresholds[0]:.2f}"
511515
else:
@@ -521,6 +525,7 @@ def plot_polarization_kde(
521525
thresholds[-1] + 0.05,
522526
0.5,
523527
f"Prob (difference {threshold_string} ) = {probability:.1f}%",
528+
fontsize=FONTSIZE,
524529
)
525530

526531
ax.set_title(f"Polarization KDE for {candidate_name}", fontsize=TITLESIZE)
@@ -587,13 +592,14 @@ def plot_kdes(sampled_voting_prefs, group_names, candidate_names, plot_by="candi
587592
else:
588593
raise ValueError("plot_by must be 'group' or 'candidate' (default: 'candidate')")
589594

595+
middle_plot = int(np.floor(num_plots / 2))
590596
for plot_idx in range(num_plots):
591597
if num_plots > 1:
592598
ax = axes[plot_idx]
593-
ax.set_ylabel("Density", fontsize=FONTSIZE)
599+
axes[middle_plot].set_ylabel("Probability Density", fontsize=FONTSIZE)
594600
else:
595601
ax = axes
596-
ax.set_ylabel("Probability Density", fontsize=FONTSIZE)
602+
axes.set_ylabel("Probability Density", fontsize=FONTSIZE)
597603
ax.set_title(f"Support {support} " + titles[plot_idx], fontsize=TITLESIZE)
598604
ax.set_xlim((0, 1))
599605
size_ticks(ax, "x")
@@ -609,11 +615,12 @@ def plot_kdes(sampled_voting_prefs, group_names, candidate_names, plot_by="candi
609615
color=colors[kde_idx],
610616
linewidth=0,
611617
)
618+
ax.set_ylabel("")
612619

613620
if num_plots > 1:
614-
axes[0].legend(bbox_to_anchor=(1, 1), loc="upper left")
621+
axes[middle_plot].legend(bbox_to_anchor=(1, 1), loc="upper left", prop={"size": 12})
615622
else:
616-
ax.legend()
623+
ax.legend(prop={"size": 12})
617624
return ax
618625

619626

pyei/two_by_two.py

+1
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,7 @@ def precinct_level_plot(self, ax=None, show_all_precincts=False, precinct_names=
821821
voting_prefs_group1,
822822
voting_prefs_group2,
823823
group_names=group_names,
824+
candidate=self.candidate_name,
824825
precinct_labels=precinct_names,
825826
show_all_precincts=show_all_precincts,
826827
ax=ax,

0 commit comments

Comments
 (0)