PageRenderTime 44ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/pandas/tests/test_graphics.py

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