Search code examples
pythonpython-3.xseabornseaborn-objects

Set fontsize of axis and legends in seaborn.objects


I am using seaborn.objects to create a faceted plot. I have read the documentation but setting font size argument does not do anything (Seaborn.objects Documentation)

My Df:

{'storage': {0: '4 °C',
  1: '4 °C',
  2: 'fresh',
  3: 'fresh',
  4: 'frozen',
  5: 'frozen',
  6: '4 °C',
  7: '4 °C',
  8: 'fresh',
  9: 'fresh',
  10: 'frozen',
  11: 'frozen',
  12: '4 °C',
  13: '4 °C',
  14: 'fresh',
  15: 'fresh',
  21: 'fresh'},
 'time': {0: '4 h',
  1: '6 h',
  2: '4 h',
  3: '6 h',
  4: '4 h',
  5: '6 h',
  6: '4 h',
  7: '6 h',
  8: '4 h',
  9: '6 h',
  10: '4 h',
  11: '6 h',
  12: '4 h',
  13: '6 h',
  14: '4 h',
  15: '6 h',
  21: '6 h'},
 'Predicted Class': {0: 'resting',
  1: 'resting',
  2: 'resting',
  3: 'resting',
  4: 'resting',
  5: 'resting',
  6: 'swollen',
  7: 'swollen',
  8: 'swollen',
  9: 'swollen',
  10: 'swollen',
  11: 'swollen',
  12: 'germling',
  13: 'germling',
  14: 'germling',
  15: 'germling',
  21: 'hyphae'},
 '%': {0: 25.57,
  1: 4.87,
  2: 12.19,
  3: 4.87,
  4: 99.87,
  5: 99.77,
  6: 72.89,
  7: 60.23,
  8: 86.93,
  9: 42.83,
  10: 0.13,
  11: 0.23,
  12: 1.54,
  13: 34.9,
  14: 0.88,
  15: 51.4,
  21: 0.9}}
import seaborn.objects as so
    
    
    # Create the Plot object
    plot = so.Plot(counts2_melted_df, x="storage", y="%", color="Predicted Class", text="%")
    
    # Facet the plot by 'strain' with an increased height for each row
    plot = plot.layout(size=(8,8)) # Adjust the scale factor for height
    
    # Add the bar and stack
    plot = plot.add(so.Bar(), so.Stack()).scale(color=color_palette)
    
    plot = plot.label(x="Storage Condition", y="Percentage of Morphotype (%)")
    
    plot= plot.theme({"axes.facecolor": "w", "axes.edgecolor": "slategray"})
    
    plot = plot.facet(row="time")
    
    #Render the plot to get the underlying matplotlib axes
    plot.plot()

I have tried

    plot = plot.label(x="Storage Condition", fontsize=28)
 plot = plot.label(y="Percentage of Morphotype (%)", fontsize=28)

Any suggestions? I need to increase the font size drastically to make the plot readable on a poster.

I would be grateful for any help!


Solution

  • You are trying to use the fontsize argument within the label method of seaborn.objects to change the font size, but this won't work because seaborn.objects has a different approach to styling and theming than traditional matplotlib or the older seaborn API.

    Instead, you need to use the .theme method to control the appearance of elements in a seaborn.objects plot, including the font size. Here's how you can do it:

    import seaborn.objects as so
    
    plot = so.Plot(counts2_melted_df, x="storage", y="%", color="Predicted Class", text="%")
    
    plot = plot.layout(size=(8, 8))
    plot = plot.add(so.Bar(), so.Stack()).scale(color=color_palette)
    plot = plot.label(x="Storage Condition", y="Percentage of Morphotype (%)")
    
    # Customize theme with larger font sizes
    plot = plot.theme({
        "axes.facecolor": "w",
        "axes.edgecolor": "slategray",
        "xtick.labelsize": 20,  # X-axis tick labels
        "ytick.labelsize": 20,  # Y-axis tick labels
        "axes.labelsize": 24,   # Axis titles
        "legend.fontsize": 18,  # Legend font size
        "axes.titleweight": "bold"  # Axis title font weight
    })
    
    plot = plot.facet(row="time")
    plot.plot()
    

    plot.theme(): This method accepts a dictionary of matplotlib-style parameters to customize various elements of the plot. I have added some general values you can use.