Styling scientific research
Now you have mastered customizing your plots it is time to let your creative energy flow!
In this exercise, you are continuing your work with the Antarctic research team to assist them to explore and understand the penguin data they have collected.
They have asked you to help them understand how the flipper length differs between species. Time is short, so you think a quick plotly.express
visualization would do. However, they also want some specific customizations for the axes' titles and a timestamp annotation when the plot is generated.
Your task is to build a quick bar chart using plotly.express
, including the specified customizations.
You have available a penguins
DataFrame and timestamp
variable which gives the current timestamp.
This exercise is part of the course
Introduction to Data Visualization with Plotly in Python
Exercise instructions
- Create a
plotly
bar chart from the penguins DataFrame with the titleFlipper Length (mm) by Species
, the x-axis asspec
, the y-axis asav_flip_length
and different colors forspec
. - Use the
.update_layout()
method to change thexaxis
andyaxis
text toSpecies
andAverage flipper length (mm)
respectively. - Add an annotation to the plot without an arrow, placed at an x coordinate of
0.5
and a y coordinate of1.1
with thetimestamp
when the plot was generated. - Show your plot!
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Create timestamp
timestamp = datetime.now()
# Create plot
fig = px.____(penguins, x=____, y=____, color="spec", title=____)
# Change the axis titles
fig.update_layout({____: {'title': {____: 'Species'}},
____: {'title': {____: 'Average Flipper Length (mm)'}}})
# Add an annotation and show
fig.update_layout({'annotations': [{
"text": f"This graph was generated at {____}",
"showarrow": False, "x": ____, "y": ____, "xref": "paper", "yref": "paper"}]})
____.____