0
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
import warnings

df = pd.DataFrame([[0.2,40,1], [0.4,50,6], [5,14,5],
                   [1,25,7], [9,23,4], [3,300,100]],
                  columns=['a1', 'a2', 'a3'])

fig, axes = plt.subplots(nrows=3, ncols=1,figsize=(18, 10), sharex=False)

sns.boxplot(ax=axes[0],data=df,y="a1")

sns.boxplot(ax=axes[1],data=df,y="a2")

sns.boxplot(ax=axes[2],data=df,y="a3")

I have found ways to put limits, for instance - set(xlim=(0,15),ylim=(0,100))) However, this isn't what I'm looking for, I'd like to have a handle on the y-axis in this data. For example - y-axis for a3 - 0, 35, 60, 100

Further, how do I set/change y-axis labels, or add sub-titles to each of grid-plots?

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Death Metal
  • 830
  • 3
  • 9
  • 26

1 Answers1

1

If I understood your question correctly, you want to show specific tick labels for y-axis. To set this, you can use set_yticks() and provide the tick labels you want to show. For giving individual y-axis labels for each subplot, use axis set_ylabel(). Similarly, you can use axis set_title() to set individual axis title. Updated code below.

import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
from scipy import stats
import warnings
df = pd.DataFrame([[0.2,40,1], [0.4,50,6], [5,14,5], [1,25,7], [9,23,4], [3,300,100]], columns=['a1', 'a2', 'a3'])
fig, axes = plt.subplots(nrows=3, ncols=1,figsize=(4, 10), sharex=False)
sns.boxplot(ax=axes[0],data=df,y="a1")
axes[0].set_yticks([0,0.5,5,10])
axes[0].set_ylabel('My A1 Label')
axes[0].set_title('My A1 Title')

sns.boxplot(ax=axes[1],data=df,y="a2")
axes[1].set_yticks([0,100,250,300])
axes[1].set_ylabel('My A2 Label')
axes[1].set_title('My A2 Title')

sns.boxplot(ax=axes[2],data=df,y="a3")
axes[2].set_yticks([0,35,60,100])
axes[2].set_ylabel('My A3 Label')
axes[2].set_title('My A3 Title')

enter image description here

Redox
  • 9,321
  • 5
  • 9
  • 26
  • Thank you. This serves the purpose. I moved from R to python. In my head `ggplot2` has one of the best colouring, graphics features aesthetically; is there a way/option/library in `python` to achieve similar quality plots? I used seaborn assuming the colours would be fine than `plotly/matplot lib`. – Death Metal Sep 14 '22 at 11:35
  • 1
    Seaborn is built on top of matplotlib and usually more flexible. As it has been around longer, you get workaround for most problems. Plotly is newer and gives newer graphs (I think). Not aware of ggplot2 as much, but is there something there that you need? May be post a question if you want to see equivalent graph in seaborn or plotly – Redox Sep 14 '22 at 11:42
  • ^ alright, I'm bent on better or comparable graphics to `ggplot2`; due to limitations I'm unable to move to R thus thought to know more about graphics/colors/aesthetics. Certainly, I'll post question as needed. Nice username BTW, reduction-oxidation~ :D – Death Metal Sep 14 '22 at 12:20
  • 1
    Have you considered using ggplot in python using [plotnine](https://realpython.com/ggplot-python/)? Not familiar with it, but this is supposed to be close ggplot2. Maybe it will help.... Thanks - I was using it based on a character in online Mech Arena game, but this is even better :D – Redox Sep 14 '22 at 13:20