Jump to content

File:Weighted median.svg

Page contents not supported in other languages.
This is a file from the Wikimedia Commons
From Wikipedia, the free encyclopedia

Original file (SVG file, nominally 383 × 515 pixels, file size: 24 KB)

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
 
This W3C-unspecified plot was created with Matplotlib.
Author Bscan
#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:
Creative Commons CC-Zero 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.

Captions

Add a one-line explanation of what this file represents

Items portrayed in this file

depicts

2 December 2013

File history

Click on a date/time to view the file as it appeared at that time.

Date/TimeThumbnailDimensionsUserComment
current02:33, 3 December 2013Thumbnail for version as of 02:33, 3 December 2013383 × 515 (24 KB)BscanMinor updates: changed data slightly to make different median more apparent and added grid lines
02:21, 3 December 2013Thumbnail for version as of 02:21, 3 December 2013386 × 513 (21 KB)BscanUser created page with UploadWizard

The following page uses this file:

Global file usage

The following other wikis use this file:

Metadata