|
| 1 | +""" |
| 2 | +============= |
| 3 | +plotfile demo |
| 4 | +============= |
| 5 | +
|
| 6 | +Replacing the deprecated `plotfile` by pandas or other matplotlib plotting |
| 7 | +methods. |
| 8 | +""" |
| 9 | + |
| 10 | +import matplotlib.pyplot as plt |
| 11 | +import matplotlib.cbook as cbook |
| 12 | + |
| 13 | +import numpy as np |
| 14 | +import pandas as pd |
| 15 | +pd.plotting.register_matplotlib_converters() |
| 16 | + |
| 17 | +# Time series. |
| 18 | +fname = cbook.get_sample_data('msft.csv', asfileobj=False) |
| 19 | +with cbook.get_sample_data('msft.csv') as file: |
| 20 | + msft = pd.read_csv(file, parse_dates=['Date']) |
| 21 | + |
| 22 | +# Use indices. |
| 23 | +plt.plotfile(fname, (0, 5, 6)) |
| 24 | + |
| 25 | +msft.plot(0, [5, 6], subplots=True) |
| 26 | + |
| 27 | +# Use names. |
| 28 | +plt.plotfile(fname, ('date', 'volume', 'adj_close')) |
| 29 | + |
| 30 | +msft.plot("Date", ["Volume", "Adj. Close*"], subplots=True) |
| 31 | + |
| 32 | +# Use semilogy for volume. |
| 33 | +plt.plotfile(fname, ('date', 'volume', 'adj_close'), |
| 34 | + plotfuncs={'volume': 'semilogy'}) |
| 35 | + |
| 36 | +fig, axs = plt.subplots(2, sharex=True) |
| 37 | +msft.plot("Date", "Volume", ax=axs[0], logy=True) |
| 38 | +msft.plot("Date", "Adj. Close*", ax=axs[1]) |
| 39 | + |
| 40 | +# Use semilogy for volume (by index). |
| 41 | +plt.plotfile(fname, (0, 5, 6), plotfuncs={5: 'semilogy'}) |
| 42 | + |
| 43 | +fig, axs = plt.subplots(2, sharex=True) |
| 44 | +msft.plot(0, 5, ax=axs[0], logy=True) |
| 45 | +msft.plot(0, 6, ax=axs[1]) |
| 46 | + |
| 47 | +# Single subplot |
| 48 | +plt.plotfile(fname, ('date', 'open', 'high', 'low', 'close'), subplots=False) |
| 49 | + |
| 50 | +msft.plot("Date", ["Open", "High", "Low", "Close"]) |
| 51 | + |
| 52 | +# Use bar for volume |
| 53 | +plt.plotfile(fname, (0, 5, 6), plotfuncs={5: "bar"}) |
| 54 | + |
| 55 | +fig, axs = plt.subplots(2, sharex=True) |
| 56 | +axs[0].bar(msft.iloc[:, 0], msft.iloc[:, 5]) |
| 57 | +axs[1].plot(msft.iloc[:, 0], msft.iloc[:, 6]) |
| 58 | +fig.autofmt_xdate() |
| 59 | + |
| 60 | +############################################################################### |
| 61 | + |
| 62 | +# Unlabeled data. |
| 63 | +fname2 = cbook.get_sample_data('data_x_x2_x3.csv', asfileobj=False) |
| 64 | +with cbook.get_sample_data('data_x_x2_x3.csv') as file: |
| 65 | + array = np.loadtxt(file) |
| 66 | + |
| 67 | +# Labeling, if no names in csv-file. |
| 68 | +plt.plotfile(fname2, cols=(0, 1, 2), delimiter=' ', |
| 69 | + names=['$x$', '$f(x)=x^2$', '$f(x)=x^3$']) |
| 70 | + |
| 71 | +fig, axs = plt.subplots(2, sharex=True) |
| 72 | +axs[0].plot(array[:, 0], array[:, 1]) |
| 73 | +axs[0].set(ylabel='$f(x)=x^2$') |
| 74 | +axs[1].plot(array[:, 0], array[:, 2]) |
| 75 | +axs[1].set(xlabel='$x$', ylabel='$f(x)=x^3$') |
| 76 | + |
| 77 | +# More than one file per figure--illustrated here with a single file. |
| 78 | +plt.plotfile(fname2, cols=(0, 1), delimiter=' ') |
| 79 | +plt.plotfile(fname2, cols=(0, 2), newfig=False, |
| 80 | + delimiter=' ') # use current figure |
| 81 | +plt.xlabel(r'$x$') |
| 82 | +plt.ylabel(r'$f(x) = x^2, x^3$') |
| 83 | + |
| 84 | +fig, ax = plt.subplots() |
| 85 | +ax.plot(array[:, 0], array[:, 1]) |
| 86 | +ax.plot(array[:, 0], array[:, 2]) |
| 87 | +ax.set(xlabel='$x$', ylabel='$f(x)=x^3$') |
| 88 | + |
| 89 | +plt.show() |
0 commit comments