1

I want to plot different plots in a single row so that it is easier for comparison. For example Distribution of a categorical feature in different data versions.

In one of my use cases I plot the target distribution changes for Target Column Species-

code:

def plot_target_ditributions(dataframes, target, versions=["Previous", "Latest"], save=False):

    nrows = 1
    ncols = len(dataframes)

    fig, axes = plt.subplots(nrows, ncols, figsize=(20, 5))

    for col in range(ncols):
        df = dataframes[col]
        ax = axes[col]
        sns.countplot(data=df, x="Species", ax=ax)
        ax.set_title(f'Target Distribution in {versions[col]} Version', fontsize = 14)

    if save:
        plt.savefig(f"./saved_plots/targetdist/target_distribution_{date.today()}", bbox_inches='tight', dpi=400)

    plt.show()

plot_target_ditributions([previous_df,intermediate_df, current_df], target,versions=["Previous" ,"Intermediate", "Latest"])

data:

The Dataset used in this example above is the Iris Dataset obtained from https://archive.ics.uci.edu/ml/datasets/iris.

Different versions of the data are simulated by adding random values for testing purposes.

previous_df data can be found here. intermediate_df data can be found here. current_df data can be found here.

The "Species" column is used in all three data sources for plotting.

enter image description here

The function is written in such a way that i can input even 10 data versions and 10 side-by-side plots will be generated.

The issue I face is that, when I increase the number of plots to be plotted, each individual plot becomes smaller.

What I want is that the Individual plot to retain its size but the output cell to expand and have a horizontal scrollable functionaility. As I remember this use to happen in my local jupyter lab and notebook but in Azure Notebooks, the horizoontal scroll is not there and the individual figure size is smaller as the number of plots increase.

What can I do to get pass this? Is there way with matplotlib to solve this or is there a solution with Azure ML Studio Notebooks itself?

Imperial_J
  • 306
  • 1
  • 7
  • 23
  • **What I want is that the Individual plot to retain its size but the output cell to expand and have a horizontal scrollable functionaility. As I remember this use to happen in my local jupyter lab and notebook** This is not the case, if you make an image that is wider than the cell, the image is shown at a reduced size. See [notebook](https://i.stack.imgur.com/6CdU9.png) vs. [saved image](https://i.stack.imgur.com/HY9mF.png) – Trenton McKinney Nov 07 '22 at 19:18
  • **Is there way with matplotlib to solve this** This is not a matplotlib/seaborn issue. This is a function of the notebook environment. If the point is for the notebook to be saved and displayed as an html document, then instead of showing the plot with `plt.show()`, you might trying saving the plot as a `.svg` file and then adding the svg file as a link in the following markdown cell using markdown or html. – Trenton McKinney Nov 07 '22 at 19:27
  • The image needs to be in a public repo and the markdown would look like `![alt text](https://raw.githubusercontent.com/trenton3983/stack_overflow/37d1ca73e76ec240f76e812911d1f56acb505d38/sample.svg)` , as an example. See [How to add images to README.md on GitHub?](https://stackoverflow.com/q/14494747/7758804) – Trenton McKinney Nov 07 '22 at 19:35
  • I tested this in jupyter lab and the image shown in the notebook is constrained by the notebook width. The difference is, if I `open image in a new tab` for the plot from `plt.show()` the image size is reduced, however the .svg image opens in a new window showing its full size with scrollbars, which you can see by looking at https://htmlpreview.github.io/?https://github.com/trenton3983/stack_overflow/blob/master/00_so_testing_py10.html – Trenton McKinney Nov 07 '22 at 19:40
  • And yes, I realize you're referring to Azure Notebooks, however, my guess, is both types of notebooks work similarly. – Trenton McKinney Nov 07 '22 at 19:48
  • If I run the same function with just two data frames. plot_target_ditributions([previous_df, current_df], target,versions=["Previous", "Latest"]), then the size of the individual plots increases. Result with three: https://gyazo.com/9f2f4c3e05fbe2a50e2d2198f5cd1867 Result with two: https://gyazo.com/604539fd677d42aa36e1d5cb8ccc4e9d – Imperial_J Nov 08 '22 at 03:24
  • 1
    The size of each countplot has changed. I want to find a way so that the size of each countplot stays the same regardless of number of dataframes passed. – Imperial_J Nov 08 '22 at 03:26
  • Its not the figure size per se, but individual plots within the figure. This is seen even when you save the image as an SVG or PNG too. – Imperial_J Nov 08 '22 at 03:27
  • I think I misunderstood your question. To clarify, you want the subplot size to be the same regardless of the number of subplots? – Trenton McKinney Nov 08 '22 at 03:31
  • Yes, subplot size to be the same . – Imperial_J Nov 08 '22 at 03:52

0 Answers0