Repository: spyder-ide/spyder

Issue:
Enhancement proposal: create a plot button in variable explorer to visualize numerical data
Hello guys, I would like to propose a plot button in variable explorer to visualize numerical data of tables, lists, dataframes, etc as in my group we deal daily with this issue, when we need to understand the meaning of some amount of data in scientific notation for example in a table, that does not make much sense in table format, but so in a graph.
I know we could use "Plots" pane in IPython console or running the code to visualize it, but would be very useful to have this implemented in a button and menu of variable explorer directly.
We did something similar to this in our internal plugin, to plot table files as you can see bellow, so if you agree _I can work on this enhancement_.
Please let me know your opinion.
![graph](https://github.com/spyder-ide/spyder/assets/19315417/766e5d94-77da-468a-beae-acdcb92604f5)


Enhancement proposal: create a plot button in variable explorer to visualize numerical data
Hello guys, I would like to propose a plot button in variable explorer to visualize numerical data of tables, lists, dataframes, etc as in my group we deal daily with this issue, when we need to understand the meaning of some amount of data in scientific notation for example in a table, that does not make much sense in table format, but so in a graph.
I know we could use "Plots" pane in IPython console or running the code to visualize it, but would be very useful to have this implemented in a button and menu of variable explorer directly.
We did something similar to this in our internal plugin, to plot table files as you can see bellow, so if you agree _I can work on this enhancement_.
Please let me know your opinion.
![graph](https://github.com/spyder-ide/spyder/assets/19315417/766e5d94-77da-468a-beae-acdcb92604f5)




Discussion:
Hey @dpturibio, thanks for reporting. This seems like a good idea to me, thanks for the suggestion!

However, in the screenshot you posted you're showing a menu entry in the context menu of the Projects pane. So, I don't understand very well why you say this should be added to the Variable explorer.
Thank you for the response @ccordoba12 .
Maybe this image caused confusion... The intention was just to show how a graph of an amount of data is useful(in this context of the image was in Projects Pane) to investigate the meaning of it. But I would like to implement this same behavior for variable explorer as in the bellow image:
![Captura de tela de 2024-01-30 20-33-17](https://github.com/spyder-ide/spyder/assets/19315417/7edf1437-6cc3-4df2-9aa5-fad56d3d04b1)
Adding an icon to plot a graph for the dataframe type as circled in red.

Clicking this icon will generate a plot in Plots Pane in the same way that the code in the screenshot did, but without the need to program and run this code, but just clicking the button.
Variable explorer already has this functionality for list types. 
![Captura de tela de 2024-01-30 21-06-21](https://github.com/spyder-ide/spyder/assets/19315417/b62ca0d5-1932-4532-aac3-a9b25614f58f)

So I would like to extend this to dataframes and maybe dictionaries.
Let me know what you think about it. 

Thanks for the clarification @dpturibio. I understand better what you want to do and agree with the idea.

However, you should wait until PR #21666 is merged before proceeding because that pull request will improve the UI for toolbars in all our editors.
Issue #21025 asks about adding a menu item for plotting histograms to the dataframe editor, so the discussion there may be relevant. As I said there, I made a quick prototype that does that. That was over half a year ago and before @dpturibio's PR #20546 got merged, so the code is no longer be current. But you can still see the commit if you're curious at https://github.com/jitseniesen/spyder/commit/05a570c22be49df1763b64070294e321c935ada9.
Hi @ccordoba12, as PR #21666 is already merged, can I start with the implementation or are you doing minor changes yet? @jitseniesen, any comments? By the way, thank you for sharing your commit in previous message.
@dpturibio, you can proceed with the improvements you want to do now.
Hey @dpturibio, thanks for reporting. This seems like a good idea to me, thanks for the suggestion!

However, in the screenshot you posted you're showing a menu entry in the context menu of the Projects pane. So, I don't understand very well why you say this should be added to the Variable explorer.
Thank you for the response @ccordoba12 .
Maybe this image caused confusion... The intention was just to show how a graph of an amount of data is useful(in this context of the image was in Projects Pane) to investigate the meaning of it. But I would like to implement this same behavior for variable explorer as in the bellow image:
![Captura de tela de 2024-01-30 20-33-17](https://github.com/spyder-ide/spyder/assets/19315417/7edf1437-6cc3-4df2-9aa5-fad56d3d04b1)
Adding an icon to plot a graph for the dataframe type as circled in red.

Clicking this icon will generate a plot in Plots Pane in the same way that the code in the screenshot did, but without the need to program and run this code, but just clicking the button.
Variable explorer already has this functionality for list types. 
![Captura de tela de 2024-01-30 21-06-21](https://github.com/spyder-ide/spyder/assets/19315417/b62ca0d5-1932-4532-aac3-a9b25614f58f)

So I would like to extend this to dataframes and maybe dictionaries.
Let me know what you think about it. 

Thanks for the clarification @dpturibio. I understand better what you want to do and agree with the idea.

However, you should wait until PR #21666 is merged before proceeding because that pull request will improve the UI for toolbars in all our editors.
Issue #21025 asks about adding a menu item for plotting histograms to the dataframe editor, so the discussion there may be relevant. As I said there, I made a quick prototype that does that. That was over half a year ago and before @dpturibio's PR #20546 got merged, so the code is no longer be current. But you can still see the commit if you're curious at https://github.com/jitseniesen/spyder/commit/05a570c22be49df1763b64070294e321c935ada9.
Hi @ccordoba12, as PR #21666 is already merged, can I start with the implementation or are you doing minor changes yet? @jitseniesen, any comments? By the way, thank you for sharing your commit in previous message.
@dpturibio, you can proceed with the improvements you want to do now.

Resolving patch:
diff --git a/spyder/widgets/collectionseditor.py b/spyder/widgets/collectionseditor.py
--- a/spyder/widgets/collectionseditor.py
+++ b/spyder/widgets/collectionseditor.py
@@ -888,6 +888,10 @@ def get_len(self, key):
         """Return sequence length"""
         raise NotImplementedError
 
+    def is_data_frame(self, key):
+        """Return True if variable is a pandas dataframe"""
+        raise NotImplementedError
+
     def is_array(self, key):
         """Return True if variable is a numpy array"""
         raise NotImplementedError
@@ -987,7 +991,10 @@ def indexes_in_same_row():
                 key = self.source_model.get_key(index)
             is_list = self.is_list(key)
             is_array = self.is_array(key) and self.get_len(key) != 0
-            condition_plot = (is_array and len(self.get_array_shape(key)) <= 2)
+            is_dataframe = self.is_data_frame(key) and self.get_len(key) != 0
+            condition_plot = (
+                is_array and len(self.get_array_shape(key)) <= 2
+                ) or is_dataframe
             condition_hist = (is_array and self.get_array_ndim(key) == 1)
             condition_imshow = condition_plot and self.get_array_ndim(key) == 2
             condition_imshow = condition_imshow or self.is_image(key)
@@ -1518,6 +1525,11 @@ def get_len(self, key):
         else:
             return len(data[key])
 
+    def is_data_frame(self, key):
+        """Return True if variable is a pandas dataframe"""
+        data = self.source_model.get_data()
+        return isinstance(data[key], pd.DataFrame)
+
     def is_array(self, key):
         """Return True if variable is a numpy array"""
         data = self.source_model.get_data()


Test patch: