fig = ot_dataframe(ĭf2img.save_dataframe(fig=fig, filename="plot.png") You can apply formatting, such as background colors or alternating the row colors for better readability. It's a wrapper/convenience function using plotly as backend. There is a Python library called df2img available at (disclaimer: I'm the author). 'wkhtmltoimage -f png -width 0 table2.html table2.png', shell=True)Īnd its aspect could be further tweaked with CSS similarly to the other approach. One would also need to output to disk the HTML file. Wkhtmltoimage can be simply called using subprocess (or any other similar means of running an external program in Python). There is also a Python package, pdfkit, that serves as a front-end to it (it does not waive you from installing the core software yourself), but I will not use it. This approach uses an external open source tool and this needs to be installed prior to the generation of the image. Html.write_png(img_filepath, stylesheets=) The code follows: import weasyprint as wspĭef trim(source_filepath, target_filepath=None, background=None):īorder = (img.mode, img.size, background)ĭiff = (img, border)Ĭss = One also would need to make sure that the image will be large enough, and this can be done with CSS's size property. One shortcoming of weasyprint is that it does not seem to provide a way of adapting the image size to its content.Īnyway, removing some background from an image is relatively easy in Python / PIL, and it is implemented in the trim() function below (adapted from here). This approach uses a pip-installable package, which will allow you to do everything using the Python ecosystem. Labels = list(string.ascii_uppercase)ĭf = pd.DataFrame(np.random.randint(0, 100, size=(5, 10)), columns=labels) Np.ed(0) # just to get reproducible results from `np.random` We can generate one with the following code: import string Let us assume we have a dataframe named df. Popular options for HTML-to-image rendering include: The final appearance could be tweaked via CSS. The best solution to your problem is probably to first export your dataframe to HTML and then convert it using an HTML-to-image tool. The ix indexer has now been fully deprecated so we should use the loc indexer instead. Is now deprecated in newer versions of pandas in favour of: from otting import table EDIT: DEPRECATION WARNINGSĪs pointed out in the comments, the import statement for table: from import table Your not-so-pretty but totally functional multi-indexed table. Now call the table function but set all the row labels in the table to the empty string (this makes sure the actual indexes of your plot are not displayed): table(ax, df, rowLabels=*df.shape, loc='center') New_cols = '','' # since my index columns are the two left-most on the table Remove all duplicates from the higher order multi-index columns by setting them to an empty string (in my example I only have duplicate indexes in "first"): df.ix = '' # see deprecation warnings belowĬhange the column names over your "indexes" to the empty string new_cols = df.columns.values If you have a multi-index data frame called df that looks like: first secondįirst reset the indexes so they become normal columns df = df.reset_index() Here is a (admittedly quite hacky) way of simulating multi-indexes when plotting using the method above. The output might not be the prettiest but you can find additional arguments for the table() function here.Īlso thanks to this post for info on how to remove axes in matplotlib. Table(ax, df) # where df is your data frame However, these can be removed first: import matplotlib.pyplot as pltįrom import table # EDIT: see deprecation warnings belowĪx = plt.subplot(111, frame_on=False) # no visible frameĪx.t_visible(False) # hide the x axisĪx.t_visible(False) # hide the y axis Usually this plots the table directly onto a plot (with axes and everything) which is not what you want. Pandas allows you to plot tables using matplotlib (details here).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |