12

I use python 3.4, pandas 0.14.1 and XlsxWriter 0.5.6. I create a graph called 'graph' using pandas with the following code

 graph=data_iter['_DiffPrice'].hist() 

, which produces a beautiful histogram.

Now, how do I insert that graph into an Excel file using XlsxWriter?

I tried the XlsxWriter method

workbook.add_chart()

but this creates a graph in Excel, not what I want.

Thanks

Charles
  • 613
  • 2
  • 8
  • 18
  • 1
    pandas uses matplotlib for plotting. [This answer](http://stackoverflow.com/a/15177991/2948808) should help you. – RJT Jul 23 '14 at 10:05
  • Yes this helps. So the strategy is to save the graph on disk first, and then to paste it as an image into the Excel file. It worked. Thanks RJT. – Charles Jul 23 '14 at 10:19

1 Answers1

24

If you would like to export Pandas data as charts in Excel using XlsxWriter then have a look at the following how-to (that I wrote): Using Pandas and XlsxWriter to create Excel charts.

enter image description here

If on the other hand you want the matplotlib style charts generated by Pandas then export them as images and insert them into a worksheet using the XlsxWriter insert_image() method.

See also Working with Python Pandas and XlsxWriter.

jmcnamara
  • 38,196
  • 6
  • 90
  • 108
  • Thanks!In my case, the second strategy (export then paste with `insert_image()`) is what I need. But it's good to know that there's an alternative approach. – Charles Jul 24 '14 at 06:18
  • @jmcnamara is there a way to incorporate plotly or seaborn with xlsxwriter? I have 2 separate datasets in a single excel sheet. How would I navigate to the cells and create the graph output? Thank you – Lynn Mar 10 '21 at 11:10
  • 1
    @Lynn AFAIK there isn't anything like that. The options for Excel charts and Plotly/Seaborn charts/plots are very different. Maybe post a question about it. – jmcnamara Mar 11 '21 at 07:24