Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option to show the scores as percentages. #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
49 changes: 29 additions & 20 deletions pyplutchik.py
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,7 @@ def _petal_shape_dyad(ax, emotion_score, colorA, colorB, angle, font, fontweight
return ax


def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontweight, fontsize, highlight = 'all', offset = .15):
def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontweight, fontsize, label_format, highlight = 'all', offset = .15):
"""
Draw the spine beneath a petal, and the annotation of emotion and emotion's value.
The spine is a straight line from the center, of length 1.03.
Expand Down Expand Up @@ -729,6 +729,9 @@ def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontwei
*highlight*:
String. 'opaque' if the petal must be shadowed, 'regular' is default.

*label_format*:
String. 'value' if the label is a value, "percentage" if the label is a percent, 'value' is default.

*offset*:
Central neutral circle has radius = .15, and petals must start from there.

Expand Down Expand Up @@ -756,6 +759,7 @@ def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontwei
except:
iterable = False

format_str = "{:.0%}" if label_format == "percent" else "{:.2f}"

if iterable:
# Label
Expand All @@ -766,17 +770,17 @@ def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontwei

# Score 1
p5 = _rotate_point((0, 1.07 + step + offset), angle)
ax.annotate("{0:.2f}".format(round(emotion_score[0],2)), xy = p5, rotation = angle2, ha='center', va = 'center',
ax.annotate(format_str.format(round(emotion_score[0],2)), xy = p5, rotation = angle2, ha='center', va = 'center',
color = color, fontfamily = font, size = fontsize, fontweight = 'regular', alpha = alpha)

# Score 2
p6 = _rotate_point((0, 1.17 + step + offset), angle)
ax.annotate("{0:.2f}".format(round(emotion_score[1],2)), xy = p6, rotation = angle2, ha='center', va = 'center',
ax.annotate(format_str.format(round(emotion_score[1],2)), xy = p6, rotation = angle2, ha='center', va = 'center',
color = color, fontfamily = font, size = fontsize, fontweight = 'demibold', alpha = alpha)

# Score 3
p7 = _rotate_point((0, 1.27 + step + offset), angle)
ax.annotate("{0:.2f}".format(round(emotion_score[2],2)), xy = p7, rotation = angle2, ha='center', va = 'center',
ax.annotate(format_str.format(round(emotion_score[2],2)), xy = p7, rotation = angle2, ha='center', va = 'center',
color = color, fontfamily = font, size = fontsize, fontweight = 'regular', alpha = alpha)

else:
Expand All @@ -788,12 +792,12 @@ def _petal_spine_emotion(ax, emotion, emotion_score, color, angle, font, fontwei

# Score
p5 = _rotate_point((0, 1.1 + step + offset), angle)
ax.annotate("{0:.2f}".format(round(emotion_score,2)), xy = p5, rotation = angle2, ha='center', va = 'center',
ax.annotate(format_str.format(round(emotion_score,2)), xy = p5, rotation = angle2, ha='center', va = 'center',
color = color, fontfamily = font, size = fontsize, fontweight = 'demibold', alpha = alpha)



def _petal_spine_dyad(ax, dyad, dyad_score, color, emotion_names, angle, font, fontweight, fontsize, highlight = 'all', offset = .15):
def _petal_spine_dyad(ax, dyad, dyad_score, color, emotion_names, angle, font, fontweight, fontsize, label_format, highlight = 'all', offset = .15):
"""
Draw the spine beneath a petal, and the annotation of dyad and dyad's value.
The spine is a straight line from the center, of length 1.03.
Expand Down Expand Up @@ -872,6 +876,7 @@ def _petal_spine_dyad(ax, dyad, dyad_score, color, emotion_names, angle, font, f
ax.annotate(emotion_names[0], xy = p10, rotation = angle2 + 8, ha='center', va = 'center', zorder = 30,
fontfamily = font, size = fontsize * .7, fontweight = 'demibold', color = color[0])

format_str = "{:.0%}" if label_format == "percent" else "{:.2f}"

# Dyad label must be grey
color = '#363636'
Expand All @@ -884,7 +889,7 @@ def _petal_spine_dyad(ax, dyad, dyad_score, color, emotion_names, angle, font, f

# Score
p5 = _rotate_point((0, 1.1 + step + offset), angle)
ax.annotate("{0:.2f}".format(round(dyad_score,2)), xy = p5, rotation = angle2, ha='center', va = 'center',
ax.annotate(format_str.format(round(dyad_score,2)), xy = p5, rotation = angle2, ha='center', va = 'center',
color = color, fontfamily = font, size = fontsize, fontweight = 'demibold', alpha = alpha)


Expand Down Expand Up @@ -953,7 +958,7 @@ def _petal_circle(ax, petal, radius, color, inner = False, highlight = 'none', o
ax.add_patch(descartes.PolygonPatch(area, fc=(0, 0, 0, 0), ec = ecol, lw = 1.5))


def _draw_emotion_petal(ax, emotion, emotion_score, highlight_emotions, show_intensity_labels, font, fontweight, fontsize, show_coordinates, height_width_ratio, normalize = False):
def _draw_emotion_petal(ax, emotion, emotion_score, highlight_emotions, show_intensity_labels, font, fontweight, fontsize, show_coordinates, height_width_ratio, label_format, normalize = False):
"""
Draw the petal and its possible sections.
Full details at http://www.github.com/alfonsosemeraro/plutchik/tutorial.ipynb
Expand Down Expand Up @@ -1020,7 +1025,8 @@ def _draw_emotion_petal(ax, emotion, emotion_score, highlight_emotions, show_int
# Draw the line and tick behind a petal
_petal_spine_emotion(ax = ax, emotion = emotion, emotion_score = emotion_score,
color = color, angle = angle,
font = font, fontweight = fontweight, fontsize = fontsize,
font = font, fontweight = fontweight, fontsize = fontsize,
label_format=label_format,
highlight = highlight,
offset = .15)
# Draw petal
Expand All @@ -1041,6 +1047,7 @@ def _draw_emotion_petal(ax, emotion, emotion_score, highlight_emotions, show_int
_petal_spine_emotion(ax = ax, emotion = emotion, emotion_score = label,
color = color, angle = angle,
font = font, fontweight = fontweight, fontsize = fontsize,
label_format=label_format,
highlight = highlight,
offset = .15)

Expand All @@ -1054,7 +1061,7 @@ def _draw_emotion_petal(ax, emotion, emotion_score, highlight_emotions, show_int
_outer_border(ax, length, color, angle, height_width_ratio = height_width_ratio, highlight = highlight, normalize = normalize)


def _draw_dyad_petal(ax, dyad, dyad_score, font, fontweight, fontsize, show_coordinates, height_width_ratio, offset = .15, normalize = False):
def _draw_dyad_petal(ax, dyad, dyad_score, font, fontweight, fontsize, show_coordinates, label_format, height_width_ratio, offset = .15, normalize = False):
"""
Draw the petal and its possible sections.
Full details at http://www.github.com/alfonsosemeraro/plutchik/tutorial.ipynb
Expand Down Expand Up @@ -1098,7 +1105,8 @@ def _draw_dyad_petal(ax, dyad, dyad_score, font, fontweight, fontsize, show_coor
_petal_spine_dyad(ax = ax, dyad = dyad, dyad_score = dyad_score,
emotion_names = emos,
color = color, angle = angle,
font = font, fontweight = fontweight, fontsize = fontsize,
font = font, fontweight = fontweight, fontsize = fontsize,
label_format=label_format,
highlight = 'all',
offset = .15)

Expand All @@ -1110,7 +1118,6 @@ def _draw_dyad_petal(ax, dyad, dyad_score, font, fontweight, fontsize, show_coor





def _check_scores_kind(tags):
"""
Expand Down Expand Up @@ -1185,7 +1192,8 @@ def plutchik(scores,
fontsize = 15,
show_coordinates = True,
show_ticklabels = False,
highlight_emotions = 'all',
highlight_emotions = 'all',
label_format="value",
show_intensity_labels = 'none',
ticklabels_angle = 0,
ticklabels_size = 11,
Expand Down Expand Up @@ -1258,7 +1266,7 @@ def plutchik(scores,
"""

scores = {key.lower(): val for key, val in scores.items()}

# Check if dyads or emotions, and what kind of dyads
score_is_emotions = _check_scores_kind(scores)
if score_is_emotions:
Expand All @@ -1275,15 +1283,16 @@ def plutchik(scores,
font = 'sans-serif'





# Draw coordinates (if needed) before any petal
if show_coordinates:
_polar_coordinates(ax, font, fontweight, fontsize, show_ticklabels, ticklabels_angle, ticklabels_size)

# Draw inner white circle
_neutral_central_circle(ax)


# Emotions and dyads are mutually exclusive
if emotions:
emotions = {key.lower(): val for key, val in emotions.items()}
Expand All @@ -1303,7 +1312,7 @@ def plutchik(scores,
_draw_emotion_petal(ax, emotion_score = emotions[emo], emotion = emo,
font = font, fontweight = fontweight, fontsize = fontsize,
highlight_emotions = highlight_emotions, show_intensity_labels = show_intensity_labels,
show_coordinates = show_coordinates, height_width_ratio = height_width_ratio, normalize = normalize)
show_coordinates = show_coordinates, height_width_ratio = height_width_ratio, label_format=label_format, normalize = normalize)

elif dyads:

Expand All @@ -1317,7 +1326,8 @@ def plutchik(scores,
# Draw dyad bicolor petal
_draw_dyad_petal(ax, dyad_score = dyads[dyad], dyad = dyad,
font = font, fontweight = fontweight, fontsize = fontsize,
show_coordinates = show_coordinates, height_width_ratio = height_width_ratio,
show_coordinates = show_coordinates, height_width_ratio = height_width_ratio,
label_format=label_format,
normalize = normalize)


Expand All @@ -1331,7 +1341,6 @@ def plutchik(scores,
c = plt.Circle((0, 0), 1.60, color = 'grey', alpha = .3, fill = False, zorder = -20, linestyle = 'dotted' )
ax.add_artist(c)


# Adjusting printable area size
lim = 1.6 if show_coordinates else 1.2
lim = lim + 0.1 if dyads else lim
Expand Down