File:Weighted median.svg
Summary
| Description |
English: Two charts showing the difference between an ordinary median and a weighted median. The weight of the element in the list is are indicated by the widths of the boxes and the value by height. The median element is shown in red. |
| Date | |
| Source | Own work |
| Author | Bscan |
| SVG development |
Source code

This media was created with Matplotlib (comprehensive library for creating static, animated, and interactive visualizations in Python)Category:Images with Matplotlib source code
Here is a listing of the source used to create this file.
Here is a listing of the source used to create this file.
#Python code shown below
#This code is issued under the Creative Commons CC0 "License"
from pylab import *
#Arbitrary values that result in a good illustrative example
data = array([1,3,4,5,8,8.5,9,10.5,11])
weights = array([0.24, 0.60, 0.33,0.28, 0.2, 0.39, 0.2, 0.1, 0.18])
bar_spacing = 0.02 #Distance between adjacent bars
#Non-medians are blue and the median is red
colors = ['b','b','b','b','r','b','b','b','b']
colorsw = ['b','b','b','r','b','b','b','b','b']
#normalize the weights, only needed to get the two plots to line up
weights = weights / sum(weights)
unweights = ones(data.__len__())/data.__len__() #Equal weighting
#The locations start at zero and are spaced by the width
locations = concatenate((array([0]),cumsum(bar_spacing + unweights[:-1])))
locations_w = concatenate((array([0]),cumsum(bar_spacing + weights[:-1])))
figure()
subplot(2,1,1)
bar(locations, data, width=unweights, color = colors)
ylabel('Value')
tick_params(axis='x', which='both',bottom='off',top='off',labelbottom='off')
xlim([-0.02, sum(unweights+0.02)])
grid()
xticks([])
subplot(2,1,2)
bar(locations_w, data, width=weights, color = colorsw)
ylabel('Value')
tick_params(axis='x', which='both',bottom='off',top='off',labelbottom='off')
xlim([-0.02, sum(weights+0.02)])
grid()
xticks([])
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
| This file is made available under the Creative Commons CC0 1.0 Universal Public Domain Dedication. | |
| The person who associated a work with this deed has dedicated the work to the public domain by waiving all of their rights to the work worldwide under copyright law, including all related and neighboring rights, to the extent allowed by law. You can copy, modify, distribute and perform the work, even for commercial purposes, all without asking permission.
|