Created
October 19, 2015 16:03
-
-
Save TechplexEngineer/04422bec49f30805d994 to your computer and use it in GitHub Desktop.
Some Python & MatPlotLib plotting examples
This file contains hidden or 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
| """ | |
| Plot demonstrating the integral as the area under a curve. | |
| Although this is a simple example, it demonstrates some important tweaks: | |
| * A simple line plot with custom color and line width. | |
| * A shaded region created using a Polygon patch. | |
| * A text label with mathtext rendering. | |
| * figtext calls to label the x- and y-axes. | |
| * Use of axis spines to hide the top and right spines. | |
| * Custom tick placement and labels. | |
| """ | |
| import numpy as np | |
| import matplotlib.pyplot as plt | |
| from matplotlib.patches import Polygon | |
| def func(x): | |
| return (x - 3) * (x - 5) * (x - 7) + 85 | |
| a, b = 2, 9 # integral limits | |
| x = np.linspace(0, 10) | |
| y = func(x) | |
| fig, ax = plt.subplots() | |
| plt.plot(x, y, 'r', linewidth=2) | |
| plt.ylim(ymin=0) | |
| # Make the shaded region | |
| ix = np.linspace(a, b) | |
| iy = func(ix) | |
| verts = [(a, 0)] + list(zip(ix, iy)) + [(b, 0)] | |
| poly = Polygon(verts, facecolor='0.9', edgecolor='0.5') | |
| ax.add_patch(poly) | |
| plt.text(0.5 * (a + b), 30, r"$\int_a^b f(x)\mathrm{d}x$", | |
| horizontalalignment='center', fontsize=20) | |
| plt.figtext(0.9, 0.05, '$x$') | |
| plt.figtext(0.1, 0.9, '$y$') | |
| plt.grid() | |
| ax.spines['right'].set_visible(False) | |
| ax.spines['top'].set_visible(False) | |
| ax.xaxis.set_ticks_position('bottom') | |
| ax.set_xticks((a, b)) | |
| ax.set_xticklabels(('$a$', '$b$')) | |
| ax.set_yticks([]) | |
| plt.show() |
This file contains hidden or 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
| import matplotlib.pyplot as plt | |
| import matplotlib.dates as md | |
| import numpy as np | |
| from datetime import datetime, timedelta | |
| import time | |
| from dateutil import tz | |
| filename = 'temp.txt' | |
| d = np.loadtxt(filename, delimiter='\t') | |
| start = d[:,0][0] | |
| start = datetime.fromtimestamp(start) | |
| # x values | |
| dates=[(datetime.fromtimestamp(ts) - timedelta(hours=start.hour, minutes=start.minute, seconds=start.second)) for ts in d[:,0]] | |
| # print max(d[:,1]) | |
| def movingaverage (values, window): | |
| if window==0: | |
| return values | |
| weights = np.repeat(1.0, window)/window | |
| sma = np.convolve(values, weights, 'valid') | |
| return sma | |
| plt.plot(movingaverage(d[:,1],5), 'b-o')#dates, | |
| plt.ylabel('TPS') | |
| plt.xlabel('Time') | |
| plt.title("TPS "+filename) | |
| plt.minorticks_on() | |
| plt.grid() | |
| # ax=plt.gca() | |
| # xfmt = md.DateFormatter('%H:%M:%S') | |
| # ax.xaxis.set_major_formatter(xfmt) | |
| plt.show() |
This file contains hidden or 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
| import matplotlib.pyplot as plt | |
| import numpy as np | |
| with plt.xkcd(): | |
| # Based on "Stove Ownership" from XKCD by Randall Monroe | |
| # http://xkcd.com/418/ | |
| fig = plt.figure() | |
| ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) | |
| ax.spines['right'].set_color('none') | |
| ax.spines['top'].set_color('none') | |
| plt.xticks([]) | |
| plt.yticks([]) | |
| ax.set_ylim([-30, 10]) | |
| data = np.ones(100) | |
| data[70:] -= np.arange(30) | |
| plt.annotate( | |
| 'THE DAY I REALIZED\nI COULD COOK BACON\nWHENEVER I WANTED', | |
| xy=(70, 1), arrowprops=dict(arrowstyle='->'), xytext=(15, -10)) | |
| plt.plot(data) | |
| plt.xlabel('time') | |
| plt.ylabel('my overall health') | |
| fig.text( | |
| 0.5, 0.05, | |
| '"Stove Ownership" from xkcd by Randall Monroe', | |
| ha='center') | |
| # Based on "The Data So Far" from XKCD by Randall Monroe | |
| # http://xkcd.com/373/ | |
| fig = plt.figure() | |
| ax = fig.add_axes((0.1, 0.2, 0.8, 0.7)) | |
| ax.bar([-0.125, 1.0-0.125], [0, 100], 0.25) | |
| ax.spines['right'].set_color('none') | |
| ax.spines['top'].set_color('none') | |
| ax.xaxis.set_ticks_position('bottom') | |
| ax.set_xticks([0, 1]) | |
| ax.set_xlim([-0.5, 1.5]) | |
| ax.set_ylim([0, 110]) | |
| ax.set_xticklabels(['CONFIRMED BY\nEXPERIMENT', 'REFUTED BY\nEXPERIMENT']) | |
| plt.yticks([]) | |
| plt.title("CLAIMS OF SUPERNATURAL POWERS") | |
| fig.text( | |
| 0.5, 0.05, | |
| '"The Data So Far" from xkcd by Randall Monroe', | |
| ha='center') | |
| plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment