microsoft / vscode-jupyter

VS Code Jupyter extension
https://marketplace.visualstudio.com/items?itemName=ms-toolsai.jupyter
MIT License
1.3k stars 294 forks source link

How to Reduce Image Size in VS Code Jupyter Notebook Display without Altering `figsize` or `dpi`? #16092

Open DonJayamanne opened 1 month ago

DonJayamanne commented 1 month ago

Discussed in https://github.com/microsoft/vscode-jupyter/discussions/16091

Originally posted by **Mingzefei** October 2, 2024 ## Problem Description: I am currently using a Jupyter Notebook inside VS Code to generate plots using `matplotlib`. The final plots must adhere to specific `figsize` and `dpi` settings for journal submission, so I cannot alter these parameters. However, when displaying the figures in VS Code, they appear excessively large, occupying the full width of the output cell. My goal is to reduce the visual size of the image in the VS Code output cell without affecting the actual dimensions of the saved figure. Currently, the images generated by `matplotlib` are rendered to take up the entire width of the cell (as seen in the screenshots), but I would like them to be rendered at half of that size. ![image](https://github.com/user-attachments/assets/3da1b905-51e7-46b8-b7f3-12a3a812c712) ![image](https://github.com/user-attachments/assets/7e0d6471-201c-4353-83ec-465611c24804) ![image](https://github.com/user-attachments/assets/de5659fc-7975-4b5a-bcc6-6c7c0ed3e90d) ## Example Code: Here’s an example of the general code structure I’m using: ```python import matplotlib.pyplot as plt import pandas as pd # Creating a sample dataset data = { "X": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0], "Y": [0.01, 0.04, 0.09, 0.16, 0.25, 0.36, 0.49, 0.64, 0.81, 1.00] } df = pd.DataFrame(data) # Plotting the data plt.figure(figsize=(6, 4), dpi=300) plt.plot(df["X"], df["Y"], '-o') plt.xlabel("X-axis Label") plt.ylabel("Y-axis Label") plt.show() ``` ## What I've Tried So Far: **Using CSS/HTML scaling in the notebook**: I tried injecting custom CSS through IPython.display.display() and HTML() to modify the height of the images in the output cell. ```python display(HTML("")) ``` but it doesn't work for me. Does anyone have a workaround or solution for reducing the display size of images in VS Code Jupyter Notebook without altering the figure’s actual dimensions? Thank you for your help!
DonJayamanne commented 1 month ago

Similar to https://github.com/microsoft/vscode-jupyter/issues/16053

ale-dg commented 4 days ago

Maybe as a workaround, creating a function would work for just changing the size of the graphs on the display but leaving the required parameter as a standard, so something like this:

import pandas as pd
import matplotlib.pyplot as plt

### standard parameters
plt.rcParams['figure.figsize'] = (6, 4)
plt.rcParams['figure.dpi'] = 300

data = {
    "X": [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0],
    "Y": [0.01, 0.04, 0.09, 0.16, 0.25, 0.36, 0.49, 0.64, 0.81, 1.00]
}

df = pd.DataFrame(data)

def plot_dataframe(df, override=False):
   if override=True:
      plt.figure(figsize=(6,4), dpi=150)
      plt.plot(df.X, df.Y, '-o')
      plt.show()

   if override=False:
      plt.plot(df.X, df.Y, '-o')
      plt.show()

Hope it helps,

Best