Lesson

Colors, font, and other styling options

Learn Colors, font, and other styling options in SQLPad's Data Science in Action course with practical examples and guided lessons.

In this lesson, we will learn how to customize the appearance of our Plotly charts by modifying colors, fonts, and other styling options. Customizing the visual appearance of your charts can help make them more visually appealing and easier to understand.

Colors

Plotly provides various ways to set colors for different elements of a chart. You can use built-in color scales, or you can specify colors using different formats such as RGB, hexadecimal, or CSS color names.

Using built-in color scales

Plotly has several built-in color scales that you can use to automatically assign colors to your data points. These color scales can be applied to continuous or categorical data.

Here's an example of how to use a built-in color scale with a scatter plot:

import plotly.express as px

# Load the built-in Iris dataset
df = px.data.iris()

# Create a scatter plot with a built-in color scale
fig = px.scatter(df, x="sepal_width", y="sepal_length", color="species_id",
                 color_continuous_scale=px.colors.sequential.Plasma)

fig.show()

Specifying colors with different formats

You can also specify colors using different formats such as RGB, hexadecimal, or CSS color names. Here's an example of how to set the marker color in a scatter plot using an RGB value:

import plotly.graph_objects as go

# Load the built-in Iris dataset
df = px.data.iris()

# Create a scatter plot with custom marker colors
fig = go.Figure(go.Scatter(x=df["sepal_width"], y=df["sepal_length"],
                           mode="markers",
                           marker=dict(color="rgb(255, 0, 0)")))

fig.show()

Font

You can customize the font used in your chart by specifying the font family, size, and color.

Here's an example of how to set the font for the title, axis labels, and tick labels in a bar chart:

import plotly.graph_objects as go
import pyodide.http

# Load the Titanic dataset
df = pd.read_csv(pyodide.http.open_url('https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv'))
print(df)
# Create a bar chart with custom font settings
fig = go.Figure(go.Bar(x=df["Pclass"], y=df["Survived"],
                       text=df["Survived"],
                       textposition="outside",
                       marker=dict(color="rgb(0, 128, 128)")))

fig.update_layout(
    title=dict(text="Survivors by Class on the Titanic",
               font=dict(family="Arial, sans-serif",
                         size=24,
                         color="rgb(0, 0, 0)")),
    xaxis=dict(title="Class",
               titlefont=dict(family="Arial, sans-serif",
                              size=18,
                              color="rgb(0, 0, 0)"),
               tickfont=dict(family="Arial, sans-serif",
                             size=14,
                             color="rgb(0, 0, 0)")),
    yaxis=dict(title="Survivors",
               titlefont=dict(family="Arial, sans-serif",
                              size=18,
                              color="rgb(0, 0, 0)"),
               tickfont=dict(family="Arial, sans-serif",
                             size=14,
                             color="rgb(0, 0, 0)"))
)

fig.show()

Other styling options

Plotly provides various other styling options to customize the appearance of your charts, such as line styles, marker styles, and background colors.

Here's an example of how to customize the line style and background color in a line chart:

import plotly.graph_objects as go

# Load the built-in Airline dataset
df = px.data.gapminder()

# Filter the dataset for the year 2007
df_2007 = df[df["year"] == 2007]

# Create a line chart with custom styling
fig = go.Figure(go.Scatter(x=df_2007["gdpPercap"], y=df_2007["lifeExp"],
                           mode="lines+markers",
                           line=dict(color="rgb(0, 128, 128)", width=2, dash="dot"),
                           marker=dict(symbol="circle", size=8, color="rgb(255, 0, 0)")))

fig.update_layout(
    plot_bgcolor="rgb(230, 230, 230)",
    xaxis=dict(gridcolor="rgb(255, 255, 255)"),
    yaxis=dict(gridcolor="rgb(255, 255, 255)")
)

fig.show()

In this lesson, we have learned how to customize the appearance of Plotly charts by modifying colors, fonts, and other styling options. Customizing your charts will help you create more visually appealing and informative visualizations.

Exercises

1. Colors, font, and other styling options

Instruction

In this exercise, you will create a scatter plot using the built-in Iris dataset and customize its appearance by modifying colors, fonts, and other styling options. Follow the steps below:

My Solution

# Your solution goes here

Hint

Start by importing the required libraries and loading the dataset. Then, create a scatter plot trace with the specified marker color. Create a figure with the trace and customize the font settings using the update_layout method. Finally, display the figure using fig.show().

Solution

import plotly.graph_objects as go
import plotly.express as px

df = px.data.iris()

scatter_plot = go.Scatter(x=df['sepal_width'], y=df['sepal_length'], mode='markers', marker=dict(color='rgb(255, 0, 0)'))

fig = go.Figure(scatter_plot)

fig.update_layout(
    title=dict(text='Sepal Width vs Sepal Length', font=dict(family='Arial, sans-serif', size=18, color='rgb(0, 0, 0)')),
    xaxis=dict(title='Sepal Width', titlefont=dict(family='Arial, sans-serif', size=18, color='rgb(0, 0, 0)'), tickfont=dict(family='Arial, sans-serif', size=14, color='rgb(0, 0, 0)')),
    yaxis=dict(title='Sepal Length', titlefont=dict(family='Arial, sans-serif', size=18, color='rgb(0, 0, 0)'), tickfont=dict(family='Arial, sans-serif', size=14, color='rgb(0, 0, 0)'))
)

fig.show()