PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/pandas/tests/test_graphics.py

http://github.com/wesm/pandas
Python | 144 lines | 114 code | 30 blank | 0 comment | 9 complexity | a3dbdd716646464a57fa235b4f1b418b MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. import nose
  2. import os
  3. import string
  4. import unittest
  5. from pandas import Series, DataFrame
  6. import pandas.util.testing as tm
  7. import numpy as np
  8. from numpy.testing.decorators import slow
  9. class TestSeriesPlots(unittest.TestCase):
  10. @classmethod
  11. def setUpClass(cls):
  12. import sys
  13. if 'IPython' in sys.modules:
  14. raise nose.SkipTest
  15. try:
  16. import matplotlib as mpl
  17. mpl.use('Agg', warn=False)
  18. except ImportError:
  19. raise nose.SkipTest
  20. def setUp(self):
  21. self.ts = tm.makeTimeSeries()
  22. self.ts.name = 'ts'
  23. self.series = tm.makeStringSeries()
  24. self.series.name = 'series'
  25. @slow
  26. def test_plot(self):
  27. _check_plot_works(self.ts.plot, label='foo')
  28. _check_plot_works(self.ts.plot, use_index=False)
  29. _check_plot_works(self.ts.plot, rot=0)
  30. _check_plot_works(self.ts.plot, style='.', logy=True)
  31. _check_plot_works(self.ts[:10].plot, kind='bar')
  32. _check_plot_works(self.series[:5].plot, kind='bar')
  33. @slow
  34. def test_hist(self):
  35. _check_plot_works(self.ts.hist)
  36. _check_plot_works(self.ts.hist, grid=False)
  37. class TestDataFramePlots(unittest.TestCase):
  38. @classmethod
  39. def setUpClass(cls):
  40. import sys
  41. if 'IPython' in sys.modules:
  42. raise nose.SkipTest
  43. try:
  44. import matplotlib as mpl
  45. mpl.use('Agg', warn=False)
  46. except ImportError:
  47. raise nose.SkipTest
  48. @slow
  49. def test_plot(self):
  50. df = tm.makeTimeDataFrame()
  51. _check_plot_works(df.plot, grid=False)
  52. _check_plot_works(df.plot, subplots=True)
  53. _check_plot_works(df.plot, subplots=True, use_index=False)
  54. df = DataFrame({'x':[1,2], 'y':[3,4]})
  55. self._check_plot_fails(df.plot, kind='line', blarg=True)
  56. @slow
  57. def test_plot_bar(self):
  58. df = DataFrame(np.random.randn(6, 4),
  59. index=list(string.ascii_letters[:6]),
  60. columns=['one', 'two', 'three', 'four'])
  61. _check_plot_works(df.plot, kind='bar')
  62. _check_plot_works(df.plot, kind='bar', legend=False)
  63. _check_plot_works(df.plot, kind='bar', subplots=True)
  64. df = DataFrame(np.random.randn(10, 15),
  65. index=list(string.ascii_letters[:10]),
  66. columns=range(15))
  67. _check_plot_works(df.plot, kind='bar')
  68. df = DataFrame({'a': [0, 1], 'b': [1, 0]})
  69. _check_plot_works(df.plot, kind='bar')
  70. @slow
  71. def test_boxplot(self):
  72. df = DataFrame(np.random.randn(6, 4),
  73. index=list(string.ascii_letters[:6]),
  74. columns=['one', 'two', 'three', 'four'])
  75. df['indic'] = ['foo', 'bar'] * 3
  76. df['indic2'] = ['foo', 'bar', 'foo'] * 2
  77. _check_plot_works(df.boxplot)
  78. _check_plot_works(df.boxplot, column=['one', 'two'])
  79. _check_plot_works(df.boxplot, column=['one', 'two'],
  80. by='indic')
  81. _check_plot_works(df.boxplot, column='one', by=['indic', 'indic2'])
  82. _check_plot_works(df.boxplot, by='indic')
  83. _check_plot_works(df.boxplot, by=['indic', 'indic2'])
  84. @slow
  85. def test_hist(self):
  86. df = DataFrame(np.random.randn(100, 4))
  87. _check_plot_works(df.hist)
  88. _check_plot_works(df.hist, grid=False)
  89. @slow
  90. def test_plot_int_columns(self):
  91. df = DataFrame(np.random.randn(100, 4)).cumsum()
  92. _check_plot_works(df.plot, legend=True)
  93. def _check_plot_fails(self, f, *args, **kwargs):
  94. self.assertRaises(Exception, f, *args, **kwargs)
  95. PNG_PATH = 'tmp.png'
  96. def _check_plot_works(f, *args, **kwargs):
  97. import matplotlib.pyplot as plt
  98. fig = plt.gcf()
  99. plt.clf()
  100. ax = fig.add_subplot(211)
  101. ret = f(*args, **kwargs)
  102. assert(ret is not None) # do something more intelligent
  103. ax = fig.add_subplot(212)
  104. try:
  105. kwargs['ax'] = ax
  106. ret = f(*args, **kwargs)
  107. assert(ret is not None) # do something more intelligent
  108. except Exception:
  109. pass
  110. plt.savefig(PNG_PATH)
  111. os.remove(PNG_PATH)
  112. if __name__ == '__main__':
  113. nose.runmodule(argv=[__file__,'-vvs','-x','--pdb', '--pdb-failure'],
  114. exit=False)