0

I am aware that pandas offer the opportunity to visualize data with plots. Most of the examples I can find and even pandas docu itself use Jupyter Notebook examples.

This code doesn't work in a row python shell.

#!/usr/bin/env python3
import pandas as pd

df = pd.DataFrame({'A': range(100)})

obj = df.hist(column='A')
# array([[<AxesSubplot:title={'center':'A'}>]], dtype=object)

How can I "show" that? This scripts runs not in an IDE. It runs in a Python 3.9.10 shell interpreter in Windows "Dos-Box" on Windows 10.

Installing jupyter or transfering the data to an external service is not an option in my case.

buhtz
  • 10,774
  • 18
  • 76
  • 149
  • 1
    For what I know, `pandas` does not provide the tools for visualizing, but only to build a diagram. You will need `matplotlib.pyplot` for visualization. – Biskweet Jul 26 '22 at 11:19
  • An example about how to use that based on my MWE would be a nice answer. – buhtz Jul 26 '22 at 11:20
  • 2
    `import matplotlib.pyplot as plt` and then just use `plt.show()`, no other code is needed (which is neat). – Biskweet Jul 26 '22 at 11:24
  • It works but makes no sense. There is not "connection" between `plt` and my `obj`. – buhtz Jul 26 '22 at 11:33
  • 1
    What doesn't make sense? `plt.show()` not being linked to your `obj` histogram? The way matplotlib.pyplot works is that as soon as you call the graphing code (e.g. `plt.hist`), it creates that plot and places it in a figure. But you can layer multiple plots on top of one another in a single figure if you never close the figure. [plt.show()](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.show.html) forces the figure to be closed and then returns all the above plots on that figure (without needing to explicitly "connect" to them) – Michael S. Jul 26 '22 at 11:50

2 Answers2

1

Demonstrating a solution building on code provided by OP:

Save this as a script named save_test.py in your working directory:

import pandas as pd
df = pd.DataFrame({'A': range(100)})
the_plot_array = df.hist(column='A')
fig = the_plot_array [0][0].get_figure()
fig.savefig("output.png")

Run that script on command line using python save_test.py.
You should see it create a file called output.png in your working directory. Open the generated image with your favorite image file viewer on your machine. If you are doing this remote, download the image file and view on your local machine.

You should also be able to run those lines in succession in a interpreter if the OP prefers.

Explanation:
Solution provided based on the fact Pandas plotting uses matplotlib as the default plotting backend (which can be changed), so you can use Matplotlib's ability to save generated plots as images, combined with Wael Ben Zid El Guebsi's answer to 'Saving plots (AxesSubPlot) generated from python pandas with matplotlib's savefig' and using type() to drill down to see that pandas histogram is returned as an numpy array of arrays. (The first item in the inner array is an matplotlib.axes._subplots.AxesSubplot object, that the_plot_array [0][0] gets. The get_figure() method gets the plot from that matplotlib.axes._subplots.AxesSubplot object.)

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Wayne
  • 6,607
  • 8
  • 36
  • 93
0

Try something like this

df = pd.DataFrame({'A': list(range(100))})

df.plot(kind='line')
buhtz
  • 10,774
  • 18
  • 76
  • 149
Esa Tuulari
  • 157
  • 2