Wrapping and Gifting Your Plot

Topic Overview

Hello and welcome to the lesson on "Wrapping and Gifting Your Plot". Today, we'll learn how to create polished visualizations using Plotly Graph Objects, focusing on exporting it in various formats. By the end of this lesson, you'll be able to present your data graphically in a way that both informs and engages, making your digital storytelling more compelling.

Building the Visualization: Scatter Plot with Custom Markers

Now, let's remind ourselves what the scatter plot looks like.

import plotly.graph_objects as go

# Create figure
fig = go.Figure()

# Add scatter trace
fig.add_trace(
    go.Scatter(
        x=df['weekid'],
        y=df['week_position'],
        mode='markers',
        marker=dict(
            size=df['weeks_on_chart'],
            sizemode='area',
            sizeref=2 * max(df['weeks_on_chart']) / (40 ** 2),
            color=df['peak_position'],
            colorscale='RdYlGn_r',
            colorbar=dict(title='Peak Position')),
        text=[f"Song: {song}<br>Performer: {performer}"
              for song, performer in zip(df['song'], df['performer'])],
        hovertemplate="%{text}<br>"
                      "Date: %{x}<br>" +
                      "Position: %{y}<br>" +
                      "Weeks on Chart: %{marker.size}<br>" +
                      "<extra></extra>"
    )
)

# Update layout
fig.update_layout(
    title='Song Performance Matrix',
    xaxis_title='Date',
    yaxis_title='Chart Position',
    yaxis=dict(
        autorange="reversed",
        gridcolor='lightgray',
    ),
    xaxis=dict(gridcolor='lightgray'),
    plot_bgcolor='white',
    hoverlabel=dict(
        bgcolor="white",
        font_size=12,
    )
)

In this plot, we use various marker properties to convey additional information, such as size to indicate weeks_on_chart and color to represent peak_position. These customizations enrich the plot by visualizing complex data points in a straightforward manner.

Customizing layout properties like axis titles, direction, and background colors can significantly impact readability and visual balance, ensuring your audience focuses on the insights.

Exporting the Masterpiece: Saving and Sharing image files

Now, let's save our work in different formats to suit various purposes. The write_image method from Plotly allows for exporting your visualization into multiple image formats.

# Save as different Image formats
fig.write_image("static_chart.svg")  # SVG format
fig.write_image("static_chart.pdf")  # PDF format
fig.write_image(
    "custom_size_chart.jpg",  # JPG format
    width=1200,
    height=800,
    scale=2
)

Here's how the parameters work:

  • "static_chart.svg": Specifies the file name and format for saving. Here, it's saved as a Scalable Vector Graphics (SVG) file, which is ideal for high-quality scalable images.
  • "static_chart.pdf": By changing the file extension, you can export your plot as a PDF document.
  • "custom_size_chart.jpg": Exports the plot as a JPG image. Alongside, there's customization by adding width=1200 and height=800, defining the image dimensions for higher resolution.
  • scale=2: This parameter enhances the resolution of the JPG image by scaling its dimensions by the provided factor, ensuring clearer images, especially useful for printing purposes.
Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal