|
7 | 7 | import json
|
8 | 8 | import pandas as pd
|
9 | 9 | from pandas.util.testing import assert_series_equal
|
| 10 | +import matplotlib.pyplot as plt |
10 | 11 |
|
11 | 12 | from plotly import utils
|
12 | 13 | from plotly.grid_objs import Column
|
13 | 14 | from plotly.graph_objs import Scatter, Scatter3d, Figure, Data
|
| 15 | +from plotly.matplotlylib import Exporter, PlotlyRenderer |
| 16 | +from plotly.plotly import plot |
14 | 17 |
|
15 | 18 | ## JSON encoding
|
16 | 19 | numeric_list = [1, 2, 3]
|
17 | 20 | np_list = np.array([1, 2, 3, np.NaN, np.NAN, np.Inf, dt(2014, 1, 5)])
|
18 | 21 | mixed_list = [1, 'A', dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1),
|
19 | 22 | dt(2014, 1, 5, 1, 1, 1, 1)]
|
| 23 | +dt_list = [dt(2014, 1, 5), dt(2014, 1, 5, 1, 1, 1), |
| 24 | + dt(2014, 1, 5, 1, 1, 1, 1)] |
20 | 25 |
|
21 | 26 | df = pd.DataFrame(columns=['col 1'],
|
22 | 27 | data=[1, 2, 3, dt(2014, 1, 5), pd.NaT, np.NaN, np.Inf])
|
@@ -98,3 +103,36 @@ def test_pandas_json_encoding():
|
98 | 103 |
|
99 | 104 | j6 = json.dumps(ts.index, cls=utils._plotlyJSONEncoder)
|
100 | 105 | assert(j6 == '["2011-01-01", "2011-01-01 01:00:00"]')
|
| 106 | + |
| 107 | + |
| 108 | +def test_numpy_masked_json_encoding(): |
| 109 | + l = [1, 2, np.ma.core.masked] |
| 110 | + j1 = json.dumps(l, cls=utils._plotlyJSONEncoder) |
| 111 | + assert(j1 == '[1, 2, NaN]') |
| 112 | + assert(set(l) == set([1, 2, np.ma.core.masked])) |
| 113 | + |
| 114 | + |
| 115 | +def test_masked_constants_example(): |
| 116 | + # example from: https://gist.github.com/tschaume/d123d56bf586276adb98 |
| 117 | + data = { |
| 118 | + 'esN': [0, 1, 2, 3], |
| 119 | + 'ewe_is0': [-398.11901997, -398.11902774, |
| 120 | + -398.11897111, -398.11882215], |
| 121 | + 'ewe_is1': [-398.11793027, -398.11792966, -398.11786308, None], |
| 122 | + 'ewe_is2': [-398.11397008, -398.11396421, None, None] |
| 123 | + } |
| 124 | + df = pd.DataFrame.from_dict(data) |
| 125 | + |
| 126 | + plotopts = {'x': 'esN', 'marker': 'o'} |
| 127 | + fig, ax = plt.subplots(1, 1) |
| 128 | + df.plot(ax=ax, **plotopts) |
| 129 | + |
| 130 | + renderer = PlotlyRenderer() |
| 131 | + Exporter(renderer).run(fig) |
| 132 | + |
| 133 | + json.dumps(renderer.plotly_fig, cls=utils._plotlyJSONEncoder) |
| 134 | + |
| 135 | + jy = json.dumps(renderer.plotly_fig['data'][1]['y'], |
| 136 | + cls=utils._plotlyJSONEncoder) |
| 137 | + assert(jy == '[-398.11793026999999, -398.11792966000002, ' |
| 138 | + '-398.11786308000001, NaN]') |
0 commit comments