Created
March 23, 2016 18:32
-
-
Save meequz/47037f9bc7662cb632c9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime as dt | |
import pylab | |
import numpy | |
import matplotlib.pyplot as pyplot | |
class Plotter(object): | |
def __init__(self, plot_dict): | |
""" | |
plot_dict must be in format: | |
{'label A': {'x': [1,2,3], 'y': [6,4,5]}, | |
'label B': {'x': [1,2,3], 'y': [9,5,0]} | |
} | |
""" | |
self.plot_dict = plot_dict | |
self.styles = ['-', '--'] | |
cmap = pyplot.get_cmap('Paired') | |
self.colors = cmap(numpy.linspace(0, 1.0, len(plot_dict))) | |
def plot(self): | |
styles = self.styles[:] | |
for name, color in zip(self.plot_dict, self.colors): | |
if len(styles) == 0: | |
styles = self.styles[:] | |
style = styles.pop() | |
data = self.plot_dict[name] | |
pylab.xticks(rotation=30) | |
pylab.plot(data['x'], data['y'], | |
label=name, color=color, linewidth=3, linestyle=style) | |
legend = pylab.legend(loc='best', | |
shadow=True, | |
ncol=len(self.plot_dict) * 2, | |
prop={'size': 12} | |
) | |
for label in legend.get_lines(): | |
label.set_linewidth(4) | |
pylab.grid() | |
pylab.show() | |
def normalize(self, minimum, maximum): | |
mm_average = (maximum - minimum) / 2 | |
for name, data in self.plot_dict.items(): | |
range_ = data['y'] | |
r_average = (max(range_) - min(range_)) / 2 | |
k = mm_average / r_average | |
range_ = [k*(it-min(range_)) for it in range_] | |
data['y'] = [it+minimum for it in range_] | |
def main(): | |
dt_template = '%Y-%m-%d' | |
dates = [ | |
dt.strptime('2006-12-03', dt_template), | |
dt.strptime('2012-04-24', dt_template), | |
dt.strptime('2013-07-19', dt_template), | |
dt.strptime('2014-02-06', dt_template), | |
dt.strptime('2015-09-26', dt_template), | |
] | |
coefficients = [ | |
867 / 867, # linux.org.ru/polls/polls/1681495 | |
1775 / 1775, # linux.org.ru/polls/polls/7678664 | |
1809 / 2625, # linux.org.ru/polls/polls/9380184 | |
966 / 1491, # linux.org.ru/polls/polls/10144916 | |
2031 / 2031, # linux.org.ru/polls/polls/11977106 | |
] | |
# real data | |
data = { | |
'gnome': {'x': dates, 'y': [23, 9, 12, 12, 12]}, | |
'kde': {'x': dates, 'y': [43, 33, 38, 32, 11]}, | |
'xfce': {'x': dates, 'y': [9, 11, 21, 25, 17]}, | |
'other': {'x': dates, 'y': [6, 14, 10, 9, 6]}, | |
'unity': {'x': dates, 'y': [None, 13, 10, 19, 9]}, | |
'lxde,razorqt,lxqt': {'x': dates, 'y': [None, 4, 7, 11, 2]}, | |
'gnome:old,forks': {'x': dates, 'y': [None, 14, 14, 18, 12]}, | |
'kde:old,forks': {'x': dates, 'y': [None, 2, 2, None, 17]}, | |
'non-tiling wm': {'x': dates, 'y': [18, None, 14, None, None]}, | |
'tiling wm': {'x': dates, 'y': [None, None, 18, 20, None]}, | |
} | |
# data with Nones replaced by approximate values | |
data = { | |
'gnome': {'x': dates, 'y': [23, 9, 12, 12, 12]}, | |
'kde': {'x': dates, 'y': [43, 33, 38, 32, 11]}, | |
'xfce': {'x': dates, 'y': [9, 11, 21, 25, 17]}, | |
'other': {'x': dates, 'y': [6, 14, 10, 9, 6]}, | |
'unity': {'x': dates, 'y': [13, 13, 10, 19, 9]}, | |
'lxde,razorqt,lxqt': {'x': dates, 'y': [4, 4, 7, 11, 2]}, | |
'gnome:old,forks': {'x': dates, 'y': [14, 14, 14, 18, 12]}, | |
'kde:old,forks': {'x': dates, 'y': [2, 2, 2, 9, 17]}, | |
'non-tiling wm': {'x': dates, 'y': [18, 16, 14, 14, 14]}, | |
'tiling wm': {'x': dates, 'y': [18, 18, 18, 20, 20]}, | |
} | |
for de_name, plotdata in data.items(): | |
for idx, k in enumerate(coefficients): | |
value = plotdata['y'][idx] | |
if value is not None: | |
plotdata['y'][idx] = k * value | |
plotter = Plotter(data) | |
plotter.plot() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment