|
1 | 1 | """
|
2 |
| -============= |
3 |
| -plotfile demo |
4 |
| -============= |
| 2 | +========================= |
| 3 | +Plotting data from a file |
| 4 | +========================= |
5 | 5 |
|
6 |
| -Replacing the deprecated `plotfile` by pandas or other matplotlib plotting |
7 |
| -methods. |
| 6 | +Plotting data from a file is actually a two-step process. |
| 7 | +
|
| 8 | +1. Interpreting the file and loading the data. |
| 9 | +2. Creating the actual plot. |
| 10 | +
|
| 11 | +`.pyplot.plotfile` tried to do both at once. But each of the steps has so many |
| 12 | +possible variations and parameters that it does not make sense to squeeze both |
| 13 | +into a single function. Therefore, `.pyplot.plotfile` has been deprecated. |
| 14 | +
|
| 15 | +The recommended way of plotting data from a file is therefore to use dedicated |
| 16 | +functions such as `numpy.loadtxt` or `pandas.read_csv` to read the data. These |
| 17 | +are more powerful and faster. Then plot the obtained data using matplotlib. |
| 18 | +
|
| 19 | +Note that `pandas.DataFrame.plot` is a convenient wrapper around Matplotlib |
| 20 | +to create simple plots. |
8 | 21 | """
|
9 | 22 |
|
10 | 23 | import matplotlib.pyplot as plt
|
11 | 24 | import matplotlib.cbook as cbook
|
12 | 25 |
|
13 | 26 | import numpy as np
|
14 | 27 | import pandas as pd
|
15 |
| -pd.plotting.register_matplotlib_converters() |
16 | 28 |
|
17 |
| -# Time series. |
| 29 | +############################################################################### |
| 30 | +# Using pandas |
| 31 | +# ============ |
| 32 | +# |
| 33 | +# Subsequent are a few examples of how to replace `~.pyplot.plotfile` with |
| 34 | +# `pandas`. All examples need the the `pandas.read_csv` call first. Note that |
| 35 | +# you can use the filename directly as a parameter:: |
| 36 | +# |
| 37 | +# msft = pd.read_csv('msft.csv') |
| 38 | +# |
| 39 | +# The following slightly more involved `pandas.read_csv` call is only to make |
| 40 | +# automatic rendering of the example work: |
| 41 | + |
18 | 42 | fname = cbook.get_sample_data('msft.csv', asfileobj=False)
|
| 43 | +with cbook.get_sample_data('msft.csv') as file: |
| 44 | + msft = pd.read_csv(file) |
| 45 | + |
| 46 | +############################################################################### |
| 47 | +# When working with dates, additionally call |
| 48 | +# `pandas.plotting.register_matplotlib_converters` and use the ``parse_dates`` |
| 49 | +# argument of `pandas.read_csv`:: |
| 50 | + |
| 51 | +pd.plotting.register_matplotlib_converters() |
| 52 | + |
19 | 53 | with cbook.get_sample_data('msft.csv') as file:
|
20 | 54 | msft = pd.read_csv(file, parse_dates=['Date'])
|
21 | 55 |
|
22 |
| -# Use indices. |
| 56 | + |
| 57 | +############################################################################### |
| 58 | +# Use indices |
| 59 | +# ----------- |
| 60 | + |
| 61 | +# Deprecated: |
23 | 62 | plt.plotfile(fname, (0, 5, 6))
|
24 | 63 |
|
| 64 | +# Use instead: |
25 | 65 | msft.plot(0, [5, 6], subplots=True)
|
26 | 66 |
|
27 |
| -# Use names. |
| 67 | +############################################################################### |
| 68 | +# Use names |
| 69 | +# --------- |
| 70 | + |
| 71 | +# Deprecated: |
28 | 72 | plt.plotfile(fname, ('date', 'volume', 'adj_close'))
|
29 | 73 |
|
| 74 | +# Use instead: |
30 | 75 | msft.plot("Date", ["Volume", "Adj. Close*"], subplots=True)
|
31 | 76 |
|
32 |
| -# Use semilogy for volume. |
| 77 | +############################################################################### |
| 78 | +# Use semilogy for volume |
| 79 | +# ----------------------- |
| 80 | + |
| 81 | +# Deprecated: |
33 | 82 | plt.plotfile(fname, ('date', 'volume', 'adj_close'),
|
34 | 83 | plotfuncs={'volume': 'semilogy'})
|
35 | 84 |
|
| 85 | +# Use instead: |
36 | 86 | fig, axs = plt.subplots(2, sharex=True)
|
37 | 87 | msft.plot("Date", "Volume", ax=axs[0], logy=True)
|
38 | 88 | msft.plot("Date", "Adj. Close*", ax=axs[1])
|
39 | 89 |
|
40 |
| -# Use semilogy for volume (by index). |
| 90 | + |
| 91 | +############################################################################### |
| 92 | +# Use semilogy for volume (by index) |
| 93 | +# ---------------------------------- |
| 94 | + |
| 95 | +# Deprecated: |
41 | 96 | plt.plotfile(fname, (0, 5, 6), plotfuncs={5: 'semilogy'})
|
42 | 97 |
|
| 98 | +# Use instead: |
43 | 99 | fig, axs = plt.subplots(2, sharex=True)
|
44 | 100 | msft.plot(0, 5, ax=axs[0], logy=True)
|
45 | 101 | msft.plot(0, 6, ax=axs[1])
|
46 | 102 |
|
| 103 | +############################################################################### |
47 | 104 | # Single subplot
|
| 105 | +# -------------- |
| 106 | + |
| 107 | +# Deprecated: |
48 | 108 | plt.plotfile(fname, ('date', 'open', 'high', 'low', 'close'), subplots=False)
|
49 | 109 |
|
| 110 | +# Use instead: |
50 | 111 | msft.plot("Date", ["Open", "High", "Low", "Close"])
|
51 | 112 |
|
| 113 | +############################################################################### |
52 | 114 | # Use bar for volume
|
| 115 | +# ------------------ |
| 116 | + |
| 117 | +# Deprecated: |
53 | 118 | plt.plotfile(fname, (0, 5, 6), plotfuncs={5: "bar"})
|
54 | 119 |
|
| 120 | +# Use instead: |
55 | 121 | fig, axs = plt.subplots(2, sharex=True)
|
56 | 122 | axs[0].bar(msft.iloc[:, 0], msft.iloc[:, 5])
|
57 | 123 | axs[1].plot(msft.iloc[:, 0], msft.iloc[:, 6])
|
58 | 124 | fig.autofmt_xdate()
|
59 | 125 |
|
60 | 126 | ###############################################################################
|
| 127 | +# Using numpy |
| 128 | +# =========== |
61 | 129 |
|
62 |
| -# Unlabeled data. |
63 | 130 | fname2 = cbook.get_sample_data('data_x_x2_x3.csv', asfileobj=False)
|
64 | 131 | with cbook.get_sample_data('data_x_x2_x3.csv') as file:
|
65 | 132 | array = np.loadtxt(file)
|
66 | 133 |
|
67 |
| -# Labeling, if no names in csv-file. |
| 134 | +############################################################################### |
| 135 | +# Labeling, if no names in csv-file |
| 136 | +# --------------------------------- |
| 137 | + |
| 138 | +# Deprecated: |
68 | 139 | plt.plotfile(fname2, cols=(0, 1, 2), delimiter=' ',
|
69 | 140 | names=['$x$', '$f(x)=x^2$', '$f(x)=x^3$'])
|
70 | 141 |
|
| 142 | +# Use instead: |
71 | 143 | fig, axs = plt.subplots(2, sharex=True)
|
72 | 144 | axs[0].plot(array[:, 0], array[:, 1])
|
73 | 145 | axs[0].set(ylabel='$f(x)=x^2$')
|
74 | 146 | axs[1].plot(array[:, 0], array[:, 2])
|
75 | 147 | axs[1].set(xlabel='$x$', ylabel='$f(x)=x^3$')
|
76 | 148 |
|
77 |
| -# More than one file per figure--illustrated here with a single file. |
| 149 | +############################################################################### |
| 150 | +# More than one file per figure |
| 151 | +# ----------------------------- |
| 152 | + |
| 153 | +# For simplicity of the example we reuse the same file. |
| 154 | +# In general they will be different. |
| 155 | +fname3 = fname2 |
| 156 | + |
| 157 | +# Depreacted: |
78 | 158 | plt.plotfile(fname2, cols=(0, 1), delimiter=' ')
|
79 |
| -plt.plotfile(fname2, cols=(0, 2), newfig=False, |
80 |
| - delimiter=' ') # use current figure |
| 159 | +plt.plotfile(fname3, cols=(0, 2), delimiter=' ', |
| 160 | + newfig=False) # use current figure |
81 | 161 | plt.xlabel(r'$x$')
|
82 | 162 | plt.ylabel(r'$f(x) = x^2, x^3$')
|
83 | 163 |
|
| 164 | +# Use instead: |
84 | 165 | fig, ax = plt.subplots()
|
85 | 166 | ax.plot(array[:, 0], array[:, 1])
|
86 | 167 | ax.plot(array[:, 0], array[:, 2])
|
|
0 commit comments