Change Size of plotly Graph in Python (Example)

 

Welcome again to another interesting tutorial from the stable of Statistics Globe. In this tutorial, I will show you how to change the size of your plotly graph using the Python programming language. You will find that as with many other aspects of using plotly, it is very simple and easy to adjust the size of your interactive plotly graphs.

First, though, let us get an overview of how we will do that:

Let’s now dive into it!

 

Install & Import plotly

If you have not already installed plotly in your Python programming environment, then in your preferred Python programming IDE, run the line of code below to download and install plotly; otherwise, you may skip to the next section.

pip install plotly

Next, we must import plotly, along with all its dependencies, into our Python environment, so that we can have access to all of its graph-building functions.

import plotly.express as px

 

Build a plotly Plot

We will make use of the preloaded tips dataset in the plotly library to build a scatter plot for the demonstration. In your IDE, call the dataset by running the code below.

df = px.data.tips()

You can print out the first 10 rows of the DataFrame by running:

print(df.head(10))
#   total_bill   tip    sex  smoker  day   time   size
#0       16.99  1.01  Female     No  Sun  Dinner     2
#1       10.34  1.66    Male     No  Sun  Dinner     3
#2       21.01  3.50    Male     No  Sun  Dinner     3
#3       23.68  3.31    Male     No  Sun  Dinner     2
#4       24.59  3.61  Female     No  Sun  Dinner     4
#5       25.29  4.71    Male     No  Sun  Dinner     4
#6        8.77  2.00    Male     No  Sun  Dinner     2
#7       26.88  3.12    Male     No  Sun  Dinner     4
#8       15.04  1.96    Male     No  Sun  Dinner     2
#9       14.78  3.23    Male     No  Sun  Dinner     2

We will now build a scatter plot where we will plot the total bills column against the tips column for male and female diners, and then color the points by whether they are smokers or not.

fig = px.scatter(df, 
                 x = "total_bill", 
                 y = "tip", 
                 facet_col = "sex", 
                 color = "smoker")
fig.show()

By default, plotly automatically sizes any plots or graphs that are created. However, a programmer can manually adjust the size of a plot, as we will see in the next section.
 

Change the Scatter Plot Size

Here, we are going to manually change the plot’s size by adjusting its width and height. You may run the code below in your IDE to see how that works.

fig = px.scatter(df, 
                 x = "total_bill", 
                 y = "tip", 
                 facet_col = "sex",
                 color = "smoker",
                 width = 600,
                 height = 400)
fig.show()

In the code above, we manually set the plot’s width to 600 and its height to 400 in the px.scatter() function. And you can clearly see that the plot size has been adjusted.

We can also adjust the border margins of the plot and can even color them. Using the update_layout() function, we parsed a Python dictionary to the margin = argument. In the dictionary, we specified the margin sizes for the left, right, top, and bottom margins. We also colored the margins light steel-blue.

fig = px.scatter(df, 
                 x = "total_bill", 
                 y = "tip", 
                 facet_col = "sex",
                 color = "smoker",
                 width = 600,
                 height = 400)
fig.update_layout(
    margin=dict(l=20, r=40, t=40, b=40),
    paper_bgcolor="LightSteelBlue",
)
fig.show()

With all that, our ploty plot was resized as given below.
 

 

So, that is how easy it is to adjust the size of a plotly graph and its margins using the Python programming language.

 

Video, Further Resources & Summary

Do you need more explanations on how to change the size of plotly graphs in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to change the size of plotly graphs in Python.

 

The YouTube video will be added soon.

 

I want to believe you have learned something new in this tutorial. If yes, then make sure to check out other plotly in Python programming tutorials available on Statistics Globe:

This post has shown how to change the size of plotly graphs in Python. In case you have further questions, you may leave a comment below. I will see you soon at the next one!

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top