. Advertisement .
..3..
. Advertisement .
..4..
Seaborn is one of the most comprehensive and most commonly used libraries for data visualization. It is often employed to plot the Python statistical graphs. Thanks to this, you can create informative and attractive graphics with full color schemes and eye-catching default styles.
This article focuses on discussing useful methods to change seaborn plot size.
How To Change Seaborn Plot Size
Method 1: Use The seaborn.set() Function
This function is mainly employed to control the seaborn plot’s configurations and themes. It would be best to combine this one with the rc parameter.
This combination controls the final figure’s size. A dictionary will be passed as the parameter’s value with the figure.figsize key and the required dimensions.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
"Day 2" : [1,2,8,4,3,9,5,2]})
sns.set(rc = {'figure.figsize':(15,8)})
p = sns.lineplot(data = df)
Method 2: Use The rcParams Function
The rcParams function works the same way as the seaborn.set. This one belongs to the matplotlin.pyplot module, controlling the plot’s style. You can also use the parameter as figure.figsize to change the figure’s size.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
"Day 2" : [1,2,8,4,3,9,5,2]})
from matplotlib import rcParams
rcParams['figure.figsize'] = 15,8
p = sns.lineplot(data = df)
Method 3: Use The matplotlib.pyplot.figure() Function
If you want to activate a figure, this method is useful. It would be better to plot the required plot after applying this function. The figsize parameter will also be employed to change the plot’s size.
You can pass the desired value for the plot’s width and height as well.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
"Day 2" : [1,2,8,4,3,9,5,2]})
plt.figure(figsize = (15,8))
p = sns.lineplot(data = df)
Method 4: Use The matplotlib.pyplot.gcf() Function
Use this method to get the current figure’s instance. It works compatible with the set_size_inches()
function to alter the plot’s final size. You can use it for Facetgrid objects as well.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
"Day 2" : [1,2,8,4,3,9,5,2]})
p = sns.lineplot(data = df)
plt.gcf().set_size_inches(15, 8)
Method 5: Use The aspect And Height Parameters
The aspect and height parameters are usually incorporated in various types of seaborn plots like catplot, jointplot, lmplot, and factorplot. Thus, make use of them to alter the plotted figure’s size.
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
df = pd.DataFrame({"Day 1": [7,1,5,6,3,10,5,8],
"Day 2" : [1,2,8,4,3,9,5,2]})
p = sns.factorplot(data = df,height=8, aspect=15/8)
Method 6: Use The matplotlib.pyplot.subplots() Function
The last and the most commonly used method to change the plot’s size is to use the matplotlib.pyplot.subplots()
. This function allows users to create a figure with various subplots.
With the figsize parameter, you can use tuples as arguments. This type of argument comes with the plot’s height and width. For this reason, you can change the overall size easily.
Conclusion
Figures and plots are undoubtedly built with some fault sizes with the automatically determined dimensions. However, there are various methods you can use to change them manually. Check these above solutions to deal with the issue.
Leave a comment