Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Updated violin plot example as per suggestions in issue #7251 #7360

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

Merged
merged 9 commits into from
Oct 31, 2016
Prev Previous commit
Next Next commit
Fixed recalculation of interquartile ranges
  • Loading branch information
DrNightmare committed Oct 30, 2016
commit 88a8c2e98b916d4ada3bd392ab5c4d3ece1ec166
18 changes: 8 additions & 10 deletions examples/statistics/customized_violin_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,11 @@
import numpy as np


def adjacent_values(vals):
q1, q3 = np.percentile(vals, [25, 75])
inter_quartile_range = q3 - q1

upper_adjacent_value = q3 + inter_quartile_range * 1.5
def adjacent_values(vals, q1, q3):
upper_adjacent_value = q3 + (q3 - q1) * 1.5
upper_adjacent_value = np.clip(upper_adjacent_value, q3, vals[-1])

lower_adjacent_value = q1 - inter_quartile_range * 1.5
lower_adjacent_value = q1 - (q3 - q1) * 1.5
lower_adjacent_value = np.clip(lower_adjacent_value, vals[0], q1)
return [lower_adjacent_value, upper_adjacent_value]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probabky doesn't need to be bracketed [], wonder if it can be used to remove zip* stuff in the unpacking...

Copy link
Contributor Author

@DrNightmare DrNightmare Oct 31, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid the unpacking, I could also optionally make 'whiskers' an np.array and then do:
whiskersMin, whiskersMax = whiskers[:, 0], whiskers[:, 1]

Irrespective of whether I do this or not, the brackets can be removed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I think it makes sense to do that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, updated in latest commit


Expand Down Expand Up @@ -62,10 +59,11 @@ def set_axis_style(ax, labels):
pc.set_edgecolor('black')
pc.set_alpha(1)

quartiles = (np.percentile(data, [25, 50, 75], axis=1))
medians = quartiles[1]
inter_quartile_ranges = quartiles[[0, 2]].T
whiskers = [adjacent_values(sorted_array) for sorted_array in data]
quartile1, medians, quartile3 = np.percentile(data, [25, 50, 75], axis=1)
inter_quartile_ranges = np.vstack([quartile1, quartile3]).T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this variable is no longer used and so can be removed

whiskers = [
adjacent_values(sorted_array, q1, q3)
for sorted_array, q1, q3 in zip(data, quartile1, quartile3)]

# plot whiskers as thin lines, quartiles as fat lines,
# and medians as points
Expand Down