From 73b263a963247a8d1313ec660b0ce81efea63ebd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 12:53:50 -0300 Subject: [PATCH 1/7] Add a bar plot with errors example --- examples/api/barchart_demo_error.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 examples/api/barchart_demo_error.py diff --git a/examples/api/barchart_demo_error.py b/examples/api/barchart_demo_error.py new file mode 100644 index 000000000000..90d7cdd6a171 --- /dev/null +++ b/examples/api/barchart_demo_error.py @@ -0,0 +1,15 @@ +""" +a bar plot with errorbars +""" +import matplotlib.pyplot as plt + +N = 5 +values = (20, 35, 30, 35, 27) +errors = (2, 3, 4, 1, 2) +pos = (0, 1, 2, 3, 4) + +plt.bar(left=pos, height=values, width=0.5, color='r', yerr=errors) + +plt.title("Bar plot with errors") + +plt.show() From 444eecd43476140bb1f0874a3d985cd3c7bb5753 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 13:04:42 -0300 Subject: [PATCH 2/7] Remove unecessary variable --- examples/api/barchart_demo_error.py | 1 - 1 file changed, 1 deletion(-) diff --git a/examples/api/barchart_demo_error.py b/examples/api/barchart_demo_error.py index 90d7cdd6a171..9dd189b37697 100644 --- a/examples/api/barchart_demo_error.py +++ b/examples/api/barchart_demo_error.py @@ -3,7 +3,6 @@ """ import matplotlib.pyplot as plt -N = 5 values = (20, 35, 30, 35, 27) errors = (2, 3, 4, 1, 2) pos = (0, 1, 2, 3, 4) From b0f85e7373d7c580cd1e6e8492abd1f3a726a0e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 13:15:54 -0300 Subject: [PATCH 3/7] Clen bar plot demo --- examples/api/barchart_demo.py | 40 ++++++----------------------------- 1 file changed, 7 insertions(+), 33 deletions(-) diff --git a/examples/api/barchart_demo.py b/examples/api/barchart_demo.py index e3d89b417752..f16f695c61b1 100644 --- a/examples/api/barchart_demo.py +++ b/examples/api/barchart_demo.py @@ -1,39 +1,13 @@ - -#!/usr/bin/env python -# a bar plot with errorbars -import numpy as np +""" +Demo of multiple bar plots. +""" import matplotlib.pyplot as plt -N = 5 -menMeans = (20, 35, 30, 35, 27) -menStd = (2, 3, 4, 1, 2) - -ind = np.arange(N) # the x locations for the groups -width = 0.35 # the width of the bars - -fig, ax = plt.subplots() -rects1 = ax.bar(ind, menMeans, width, color='r', yerr=menStd) - -womenMeans = (25, 32, 34, 20, 25) -womenStd = (3, 5, 2, 3, 3) -rects2 = ax.bar(ind+width, womenMeans, width, color='y', yerr=womenStd) - -# add some text for labels, title and axes ticks -ax.set_ylabel('Scores') -ax.set_title('Scores by group and gender') -ax.set_xticks(ind+width) -ax.set_xticklabels( ('G1', 'G2', 'G3', 'G4', 'G5') ) - -ax.legend( (rects1[0], rects2[0]), ('Men', 'Women') ) +values = (20, 35, 30, 35, 27) +pos = (0, 1, 2, 3, 4) -def autolabel(rects): - # attach some text labels - for rect in rects: - height = rect.get_height() - ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height), - ha='center', va='bottom') +plt.bar(left=pos, height=values, width=0.5, color='r') -autolabel(rects1) -autolabel(rects2) +plt.title('Example of bar plots') plt.show() From df4da45d43a2e5b16329301aa16db094604125d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 13:17:17 -0300 Subject: [PATCH 4/7] Improved docstring of barplot example --- examples/api/barchart_demo_error.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/api/barchart_demo_error.py b/examples/api/barchart_demo_error.py index 9dd189b37697..3cf07b9bdf35 100644 --- a/examples/api/barchart_demo_error.py +++ b/examples/api/barchart_demo_error.py @@ -1,5 +1,5 @@ """ -a bar plot with errorbars +Demo of multiple bar plot with errorbars """ import matplotlib.pyplot as plt From db76ed317562ee6b63fafddf85963c294a49204f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 13:23:27 -0300 Subject: [PATCH 5/7] Move bar plot demos to statistics folder --- examples/{api => statistics}/barchart_demo.py | 0 examples/{api => statistics}/barchart_demo_error.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename examples/{api => statistics}/barchart_demo.py (100%) rename examples/{api => statistics}/barchart_demo_error.py (100%) diff --git a/examples/api/barchart_demo.py b/examples/statistics/barchart_demo.py similarity index 100% rename from examples/api/barchart_demo.py rename to examples/statistics/barchart_demo.py diff --git a/examples/api/barchart_demo_error.py b/examples/statistics/barchart_demo_error.py similarity index 100% rename from examples/api/barchart_demo_error.py rename to examples/statistics/barchart_demo_error.py From fcc48f59aca2eeea9351d019c7fae5e45fd74693 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Sat, 29 Jun 2013 13:58:49 -0300 Subject: [PATCH 6/7] Clean two scaled y-axis demo. --- examples/api/fahrenheit_celsius_scales.py | 27 -------------- .../fahrenheit_celsius_scales.py | 35 +++++++++++++++++++ 2 files changed, 35 insertions(+), 27 deletions(-) delete mode 100644 examples/api/fahrenheit_celsius_scales.py create mode 100644 examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py diff --git a/examples/api/fahrenheit_celsius_scales.py b/examples/api/fahrenheit_celsius_scales.py deleted file mode 100644 index 72a52f4fa41b..000000000000 --- a/examples/api/fahrenheit_celsius_scales.py +++ /dev/null @@ -1,27 +0,0 @@ -""" -Show how to display two scales on the left and right y axis -- Fahrenheit and Celsius -""" - -import matplotlib.pyplot as plt - -fig, ax1 = plt.subplots() # ax1 is the Fahrenheit scale -ax2 = ax1.twinx() # ax2 is the Celsius scale - -def Tc(Tf): - return (5./9.)*(Tf-32) - - -def update_ax2(ax1): - y1, y2 = ax1.get_ylim() - ax2.set_ylim(Tc(y1), Tc(y2)) - ax2.figure.canvas.draw() - -# automatically update ylim of ax2 when ylim of ax1 changes. -ax1.callbacks.connect("ylim_changed", update_ax2) -ax1.plot([78, 79, 79, 77]) - -ax1.set_title('Two scales: Fahrenheit and Celsius') -ax1.set_ylabel('Fahrenheit') -ax2.set_ylabel('Celsius') - -plt.show() diff --git a/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py b/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py new file mode 100644 index 000000000000..e76c92a8b8c2 --- /dev/null +++ b/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py @@ -0,0 +1,35 @@ +""" +Demo of how to display two scales on the left and right y axis. + +This example uses the Fahrenheit and Celsius scales. +""" +import matplotlib.pyplot as plt +import numpy as np + +fig, ax1 = plt.subplots() # ax1 is the Fahrenheit scale +ax2 = ax1.twinx() # ax2 is the Celsius scale + +def fahrenheit2celsius(temp): + """ + Returns temperature in Celsius. + """ + return (5./9.)*(temp - 32) + +def convert_ax2_to_celsius(ax1): + """ + Update second axis according with first axis. + """ + y1, y2 = ax1.get_ylim() + ax2.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2)) + ax2.figure.canvas.draw() + +# automatically update ylim of ax2 when ylim of ax1 changes. +ax1.callbacks.connect("ylim_changed", convert_ax2_to_celsius) +ax1.plot(np.linspace(-40, 120, 100)) +ax1.set_xlim(0, 100) + +ax1.set_title('Two scales: Fahrenheit and Celsius') +ax1.set_ylabel('Fahrenheit') +ax2.set_ylabel('Celsius') + +plt.show() From 01a5e99770c06fe14816879f8ff85a4fa89c6396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gustavo=20Bragan=C3=A7a?= Date: Wed, 3 Jul 2013 01:21:16 -0300 Subject: [PATCH 7/7] Change according @tonysyu suggestions and run PEP8 --- .../fahrenheit_celsius_scales.py | 28 ++++++++++--------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py b/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py index e76c92a8b8c2..2318f1678671 100644 --- a/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py +++ b/examples/subplots_axes_and_figures/fahrenheit_celsius_scales.py @@ -6,30 +6,32 @@ import matplotlib.pyplot as plt import numpy as np -fig, ax1 = plt.subplots() # ax1 is the Fahrenheit scale -ax2 = ax1.twinx() # ax2 is the Celsius scale def fahrenheit2celsius(temp): """ Returns temperature in Celsius. """ - return (5./9.)*(temp - 32) + return (5. / 9.) * (temp - 32) -def convert_ax2_to_celsius(ax1): + +def convert_ax_c_to_celsius(ax_f): """ Update second axis according with first axis. """ - y1, y2 = ax1.get_ylim() - ax2.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2)) - ax2.figure.canvas.draw() + y1, y2 = ax_f.get_ylim() + ax_c.set_ylim(fahrenheit2celsius(y1), fahrenheit2celsius(y2)) + ax_c.figure.canvas.draw() + +fig, ax_f = plt.subplots() +ax_c = ax_f.twinx() # automatically update ylim of ax2 when ylim of ax1 changes. -ax1.callbacks.connect("ylim_changed", convert_ax2_to_celsius) -ax1.plot(np.linspace(-40, 120, 100)) -ax1.set_xlim(0, 100) +ax_f.callbacks.connect("ylim_changed", convert_ax_c_to_celsius) +ax_f.plot(np.linspace(-40, 120, 100)) +ax_f.set_xlim(0, 100) -ax1.set_title('Two scales: Fahrenheit and Celsius') -ax1.set_ylabel('Fahrenheit') -ax2.set_ylabel('Celsius') +ax_f.set_title('Two scales: Fahrenheit and Celsius') +ax_f.set_ylabel('Fahrenheit') +ax_c.set_ylabel('Celsius') plt.show()