How to set a seaborn chart size?


Seaborn is a Python data visualization library based on Matplotlib. It provides beautiful default styles and color palettes to make statistical plots more attractive. It is also closely integrated with the data structures from Pandas. Seaborn aims to make visualization the central part of exploring and understanding data.

This type of charts allows users to switch between different visual representations for the same variables for a better understanding of the dataset. Seaborn is used in various applications for visualizing the relationship between variables, examining univariate and bivariate distributions, and more. A univariate distribution shows the distribution of a single variable, while a bivariate distribution shows the joint distribution of two variables. For example, you could use a histogram to see the distribution of a single variable or a joint plot to see the joint distribution of two variables.

Syntax

The following syntax is used in the examples −

pie()

This is a built-in function that follows the seaborn to draw the pie graph for data visualization.

figure()

The figure method in Python is used to set the size of figures of different graphs based on different built-in functions like pie(), lineplot(), etc.

hist()

The hist() is the built-in method in Python that create the histogram. It is available in the module named numpy and matplotlib.

lineplot()

A line plot with many semantic groups is drawn using the line plot () method. It is accessible in the Seaborn library, a matplotlib-based Python data visualization library.

barplot()

This is an in-built function in Python that can be used to represent the bar graph.

pointplot()

This built-in method follows the seaborn and Matplotlib modules that set the circular point when x and y coordinates meet at specific point.

set()

This method collects the unordered collection of a unique element that sets some properties in it.

Example 1

In the following example, this program creates a pie chart using the matplotlib and seaborn libraries. Then style is set using sns.set(). Next, data for the pie chart is defined, including the labels and values for each slice and the colors for each region. The size of the pie chart is set using plt.figure() and then the pie chart is plotted using plt.pie() with the specified data and colors. The axis is set to equal using plt.axis() to ensure that the pie chart is displayed as a circle. Finally, the plot is displayed using plt.show().

import matplotlib.pyplot as plt
import seaborn as sns

# Set the style and color palette
sns.set(style='white')

# Data Plotting
student = 'Raju', 'Mohan', 'Shyam', 'Rita'
weight = [80, 64, 72, 51]
color_region = ['#f0bb0c', '#6a7542', '#780423', 
'#03fcfc']

# Set the size of the pie chart
plt.figure(figsize=(4,4))

# Plot to set the different color region to each student
plt.pie(weight, labels = student, colors = color_region )
plt.axis('equal')
plt.show()

Output

Example 2

In the following example, we will the seaborn chart size based on the graph named histogram. This code generates a histogram using the matplotlib, numpy, and seaborn libraries. The style is set using sns.set(). Next, random data is generated using np.random.randn() method. The size of the histogram is set using plt.figure(). The histogram is then plotted using plt.hist() with the specified data, number of bins, and color. Finally, the plot is displayed using plt.show().

import matplotlib.pyplot as plt
import numpy as np
import seaborn as sns
sns.set(style='darkgrid')

# To generate the random data by using a random method
data = np.random.randn(1000)

# Set the size of the histogram
plt.figure(figsize=(6,6))

# Plot
plt.hist(data, bins=10, color='#738050')
plt.show()

Output

Example 3

In the following example, we will show the seaborn chart size based on the barplot(). Begin the program by mentioning the necessary libraries. Then create the data for horizontal and vertical axes. Next, set the desired figure size with the help subplot and store it in the variable fig, ax. Then use the barplot that follows the seaborn module and accepts three parameters- x, y, and ax. Finally, print the result using show() method.

import seaborn as sns
import matplotlib.pyplot as plt

# create data
color = ['Blue', 'Orange', 'Green','darkpink']
color_percentage = [30, 10, 40, 60]

# Create a figure and axes with the desired size
fig, ax = plt.subplots(figsize=(6, 4))

# Plot the data
sns.barplot(x=color, y=color_percentage, ax=ax)
plt.show()

Output

Example 4

In the following example, we will show the chart size of lineplot() graph. First, mention the necessary module and then create the data for the x and y axes that meet specific points in a graph. Next, set the figure size using the built-in method subplots that sets the parameters named figsize which set width to 6 and height to 2. Then set the graph as a lineplot that accepts the parameters- x, y, and ax. This method builds the plotting line on a graph. Finally, use the method named show() to get the result.

import seaborn as sns
import matplotlib.pyplot as plt

# Data
x_axes = [10, 21, 33, 5, 11]
y_axes = [3, 11, 94, 7, 2]

# Create a figure and axes with the desired size
fig, ax = plt.subplots(figsize=(6, 2))

# Plot the data
sns.lineplot(x=x_axes, y=y_axes, ax=ax)
plt.show()

Output

Example 5

In the following example, we will generate a pointplot using the seaborn and matplotlib libraries. The data for the x and y axes are defined and a figure and axes with the desired size are created using plt.subplots(). Then plot the data using sns.pointplot() with the specified axes object. Finally, the plot is displayed using plt.show().

import seaborn as sns
import matplotlib.pyplot as plt

# Data
x_ax = ['A', 'B', 'C', 'D', 'E', 'F']
y_ax = [3, 1, 4, 9, 2, 6]

# Create a figure and axes with the desired size
fig, ax = plt.subplots(figsize=(8, 3))

# Plot the data
sns.pointplot(x=x_ax, y=y_ax, ax=ax)
plt.show()

Output

Conclusion

In the above article, we discussed the different types of charts based on seaborn libraries like pie(), histogram(), barplot(), lineplot(), and, pointplot(). All the plots have different meanings according to the requirements of the datasets. This type of figure is generally used to analyze the data of a ny company or organization.

Updated on: 17-Jul-2023

128 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements