|
2 | 2 | # coding: utf-8
|
3 | 3 |
|
4 | 4 | import nose
|
| 5 | +import itertools |
5 | 6 | import os
|
6 | 7 | import string
|
7 | 8 | from distutils.version import LooseVersion
|
@@ -138,6 +139,63 @@ def test_irregular_datetime(self):
|
138 | 139 | ax.set_xlim('1/1/1999', '1/1/2001')
|
139 | 140 | self.assertEqual(xp, ax.get_xlim()[0])
|
140 | 141 |
|
| 142 | + @slow |
| 143 | + def test_pie_series(self): |
| 144 | + # if sum of values is less than 1.0, pie handle them as rate and draw semicircle. |
| 145 | + series = Series(np.random.randint(1, 5), |
| 146 | + index=['a', 'b', 'c', 'd', 'e'], name='YLABEL') |
| 147 | + ax = _check_plot_works(series.plot, kind='pie') |
| 148 | + for t, expected in zip(ax.texts, series.index): |
| 149 | + self.assertEqual(t.get_text(), expected) |
| 150 | + self.assertEqual(ax.get_ylabel(), 'YLABEL') |
| 151 | + |
| 152 | + # without wedge labels |
| 153 | + ax = _check_plot_works(series.plot, kind='pie', labels=None) |
| 154 | + for t, expected in zip(ax.texts, [''] * 5): |
| 155 | + self.assertEqual(t.get_text(), expected) |
| 156 | + |
| 157 | + # with less colors than elements |
| 158 | + color_args = ['r', 'g', 'b'] |
| 159 | + ax = _check_plot_works(series.plot, kind='pie', colors=color_args) |
| 160 | + |
| 161 | + import matplotlib.colors as colors |
| 162 | + conv = colors.colorConverter |
| 163 | + color_expected = ['r', 'g', 'b', 'r', 'g'] |
| 164 | + for p, expected in zip(ax.patches, color_expected): |
| 165 | + self.assertEqual(p.get_facecolor(), conv.to_rgba(expected)) |
| 166 | + |
| 167 | + # with labels and colors |
| 168 | + labels = ['A', 'B', 'C', 'D', 'E'] |
| 169 | + color_args = ['r', 'g', 'b', 'c', 'm'] |
| 170 | + ax = _check_plot_works(series.plot, kind='pie', labels=labels, colors=color_args) |
| 171 | + |
| 172 | + for t, expected in zip(ax.texts, labels): |
| 173 | + self.assertEqual(t.get_text(), expected) |
| 174 | + for p, expected in zip(ax.patches, color_args): |
| 175 | + self.assertEqual(p.get_facecolor(), conv.to_rgba(expected)) |
| 176 | + |
| 177 | + # with autopct and fontsize |
| 178 | + ax = _check_plot_works(series.plot, kind='pie', colors=color_args, |
| 179 | + autopct='%.2f', fontsize=7) |
| 180 | + pcts = ['{0:.2f}'.format(s * 100) for s in series.values / float(series.sum())] |
| 181 | + iters = [iter(series.index), iter(pcts)] |
| 182 | + expected_texts = list(it.next() for it in itertools.cycle(iters)) |
| 183 | + for t, expected in zip(ax.texts, expected_texts): |
| 184 | + self.assertEqual(t.get_text(), expected) |
| 185 | + self.assertEqual(t.get_fontsize(), 7) |
| 186 | + |
| 187 | + # includes negative value |
| 188 | + with tm.assertRaises(ValueError): |
| 189 | + series = Series([1, 2, 0, 4, -1], index=['a', 'b', 'c', 'd', 'e']) |
| 190 | + series.plot(kind='pie') |
| 191 | + |
| 192 | + # includes nan |
| 193 | + series = Series([1, 2, np.nan, 4], |
| 194 | + index=['a', 'b', 'c', 'd'], name='YLABEL') |
| 195 | + ax = _check_plot_works(series.plot, kind='pie') |
| 196 | + for t, expected in zip(ax.texts, series.index): |
| 197 | + self.assertEqual(t.get_text(), expected) |
| 198 | + |
141 | 199 | @slow
|
142 | 200 | def test_hist(self):
|
143 | 201 | _check_plot_works(self.ts.hist)
|
@@ -1511,6 +1569,39 @@ def test_allow_cmap(self):
|
1511 | 1569 | df.plot(kind='hexbin', x='A', y='B', cmap='YlGn',
|
1512 | 1570 | colormap='BuGn')
|
1513 | 1571 |
|
| 1572 | + @slow |
| 1573 | + def test_pie_df(self): |
| 1574 | + df = DataFrame(np.random.rand(5, 3), columns=['X', 'Y', 'Z'], |
| 1575 | + index=['a', 'b', 'c', 'd', 'e']) |
| 1576 | + with tm.assertRaises(ValueError): |
| 1577 | + df.plot(kind='pie') |
| 1578 | + |
| 1579 | + ax = _check_plot_works(df.plot, kind='pie', y='Y') |
| 1580 | + for t, expected in zip(ax.texts, df.index): |
| 1581 | + self.assertEqual(t.get_text(), expected) |
| 1582 | + |
| 1583 | + axes = _check_plot_works(df.plot, kind='pie', subplots=True) |
| 1584 | + self.assertEqual(len(axes), len(df.columns)) |
| 1585 | + for ax in axes: |
| 1586 | + for t, expected in zip(ax.texts, df.index): |
| 1587 | + self.assertEqual(t.get_text(), expected) |
| 1588 | + for ax, ylabel in zip(axes, df.columns): |
| 1589 | + self.assertEqual(ax.get_ylabel(), ylabel) |
| 1590 | + |
| 1591 | + labels = ['A', 'B', 'C', 'D', 'E'] |
| 1592 | + color_args = ['r', 'g', 'b', 'c', 'm'] |
| 1593 | + axes = _check_plot_works(df.plot, kind='pie', subplots=True, |
| 1594 | + labels=labels, colors=color_args) |
| 1595 | + self.assertEqual(len(axes), len(df.columns)) |
| 1596 | + |
| 1597 | + import matplotlib.colors as colors |
| 1598 | + conv = colors.colorConverter |
| 1599 | + for ax in axes: |
| 1600 | + for t, expected in zip(ax.texts, labels): |
| 1601 | + self.assertEqual(t.get_text(), expected) |
| 1602 | + for p, expected in zip(ax.patches, color_args): |
| 1603 | + self.assertEqual(p.get_facecolor(), conv.to_rgba(expected)) |
| 1604 | + |
1514 | 1605 | def test_errorbar_plot(self):
|
1515 | 1606 |
|
1516 | 1607 | d = {'x': np.arange(12), 'y': np.arange(12, 0, -1)}
|
@@ -1918,6 +2009,7 @@ def _check_plot_works(f, *args, **kwargs):
|
1918 | 2009 | plt.savefig(path)
|
1919 | 2010 | finally:
|
1920 | 2011 | tm.close(fig)
|
| 2012 | + |
1921 | 2013 | return ret
|
1922 | 2014 |
|
1923 | 2015 |
|
|
0 commit comments