Plotting with Pandas: Adjusting Figure Size

Data visualization is an essential part of data analysis, and Pandas offers a versatile tool for creating tailored visualizations of data. One essential aspect of data visualization is the flexibility to adjust the figure size to make it clear, informative, and visually appealing.

This article will take you through adjusting the figure size in Pandas, using practical examples of scatter plots.

## Adjusting the Figure Size – figsize

The figsize parameter is central to resizing a figure in Pandas. This parameter takes a tuple of two values, including the width and height of the figure, in inches.

Therefore, when specifying figsize, one has to consider the size of the individual figure rendered in inches. The figsize parameter can be incorporated into the plot method of any plot in Pandas, that is, scatter plots, line plots, hist plots, etc.

Example 1: Default Size

By default, every plot in Pandas has a size of 6 inches by 4 inches. When plotting with Pandas, the figsize parameter is not required, and its omission implies that the default plot size of 6×4 will be used.

Let us consider a simple example of plotting a scatter where we omit the figsize parameter, as shown below:

“`

## import pandas as pd

import matplotlib.pyplot as plt

df = pd.read_csv(‘https://people.sc.fsu.edu/~jburkardt/data/csv/xy_points.csv’)

df.plot.scatter(x=’X’, y=’Y’)

plt.show()

“`

## The above code produces a scatter plot as shown below:

[image1]

Example 2: Horizontal Plot

To adjust the size of a horizontal scatter plot in Pandas, we can specify a larger width value in the figsize parameter. When creating horizontal plots, we should consider assigning a reasonable size to the width, which is the horizontal axis.

In this example, we shall plot a scatter of random points with the width of the figure increased to 10 inches, as shown below:

“`

## import pandas as pd

import matplotlib.pyplot as plt

## import numpy as np

x = np.random.rand(100)

y = np.random.rand(100)

df=pd.DataFrame({‘X’: x, ‘Y’: y})

df.plot.scatter(x=’X’, y=’Y’, figsize=(10,4))

plt.show()

“`

The above code produces a scatter plot with a width of 10 inches and a height of the default 4 inches, as shown below:

[image2]

Example 3: Vertical Plot

To adjust the size of a vertical scatter plot, we can specify a larger height value in the figsize parameter. When creating vertical plots, we should consider assigning a reasonable size to the height, which is the vertical axis.

In this example, we shall plot a scatter of random points with the height of the figure increased to 10 inches, as shown below:

“`

## import pandas as pd

import matplotlib.pyplot as plt

## import numpy as np

x = np.random.rand(100)

y = np.random.rand(100)

df=pd.DataFrame({‘X’: x, ‘Y’: y})

df.plot.scatter(x=’X’, y=’Y’, figsize=(4,10))

plt.show()

“`

The above code produces a scatter plot with a height of 10 inches and a width of the default 4 inches, as shown below:

[image3]

## Conclusion

In summary, Pandas provides a straightforward method of adjusting the figure size of plots using the figsize parameter. By specifying the desired width and height values of the figure in inches, we can create figures with varying heights and widths to suit our visualization preferences.

The ability to fine-tune the figure size facilitates impactful presentations that highlight insights from data, essential to making informed decisions. In conclusion, adjusting figure size is an essential aspect of data visualization using Pandas.

By using the figsize parameter, one can adjust a plot’s size to suit their preferences, making the visualization visually appealing and informative. This article demonstrated how to adjust the size of a figure in Pandas using practical examples of scatter plots.

To make impactful presentations, we should consider fine-tuning figure size to highlight insights from data, which is crucial to making informed decisions. Overall, understanding the flexibility of the figsize parameter is key to creating effective data visualizations using Pandas.