daleroberts / itermplot

An awesome iTerm2 backend for Matplotlib, so you can plot directly in your terminal.
1.49k stars 51 forks source link

Python 2.7 support #8

Closed lampts closed 7 years ago

lampts commented 7 years ago

I got this issue:

./itermplot/test.py in () 20 ax.set_title('Random graph') 21 ---> 22 plt.show()

./lib/python2.7/site-packages/matplotlib/pyplot.pyc in show(*args, *kw) 250 """ 251 global _show --> 252 return _show(args, **kw) 253 254

./itermplot/itermplot.pyc in show() 101 figmanager = Gcf.get_active() 102 if figmanager is not None: --> 103 figmanager.show() 104 else: 105 for manager in Gcf.get_all_fig_managers():

./itermplot/itermplot.pyc in show(self) 214 self.canvas.print_figure(data, facecolor='none', 215 edgecolor='none', transparent=True) --> 216 imgcat(data.getbuffer()) 217 218 FigureCanvas = FigureCanvasPdf

AttributeError: '_io.BytesIO' object has no attribute 'getbuffer'

What's wrong with that?

daleroberts commented 7 years ago

Currently I haven't backported this code to Python 2.7...

brenshanny commented 7 years ago

I've been able to get itermplot to play a little nicer with Python2.7 by changing the way the module is accessing the BytesIO data (not sure if the way I am doing it is Python3 compatable).

First I changed line 215 to imgcat(data.getvalue()) which worked in getting the BytesIO data through to the imgcat function. Then, within the imgcat function the 'utf8' is removed from line 81 where the string is added onto 'buf' (Also modify line 76 the same way). Lastly, there doesnt seem to be a 'buffer' attribute for sys.stdout, so by removing that you can get the plot to show by sys.stdout.write(buf).

Again I'm not sure how Python3 will handle this, and it's not a full solution (nor a full plot!), just partway there.

EDIT: This fix does actually show the entire plot. My terminal background is black and so are the labels and axis, so it was pretty darn hard to see them. This fix also works with Python3.

daleroberts commented 7 years ago

This should be fixed now @lampts. Thanks to @brenshanny.