Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive graph example #492

Closed

Conversation

sierrapablo
Copy link

Hi!

I made an example of interactive graphs for displaying data using plotly

The example shows laptime evolution during a race and can be filtered to compare different drivers.

As I was not sure if there was a plotly supported template, I made a custom one, but it can be removed and replaced by default plotly templates (I recommend using dark ones).

I also have a jupyter notebook version of the example, and a py script that exports the graph into an interactive html file.

Here are some screenshots of the graph:
example_interactive_graph

example_interactive_graph_2

@sierrapablo sierrapablo changed the title Interactive graph exmample Interactive graph example Dec 13, 2023
@theOehrly
Copy link
Owner

Hi, this looks really cool!

I have two things to note for now. First, as you mentioned as well, I think it might be preferable to not define a custom plotly template. In my opinion, it makes the example a bit too bloated.

Second, the example is currently not rendered correctly in the docs. There is no plotting output shown. You can compare your code to examples/plot_results_tracker.py which is another plotly based example.
Also, it might be nice to break your code into sections with short explanations as well.

@sierrapablo
Copy link
Author

I have two things to note for now. First, as you mentioned as well, I think it might be preferable to not define a custom plotly template. In my opinion, it makes the example a bit too bloated.

Sure, it's better to use a built-in template.

Second, the example is currently not rendered correctly in the docs. There is no plotting output shown. You can compare your code to examples/plot_results_tracker.py which is another plotly based example. Also, it might be nice to break your code into sections with short explanations as well.

Fixed as the example, thanks!

@sierrapablo
Copy link
Author

Also, I noticed it was not quite friendly having on the first render all of the drivers shown.
I changed visibility to 'legendonly' and then changed visibility only for two drivers.

@theOehrly
Copy link
Owner

The example is still not being rendered correctly in the documentation.

Show Image

grafik

I'm not really sure why that is the case, without digging any further right now. Ideally, you could try to build the documentation locally as that would be the quickest way to figure this out.
See https://docs.fastf1.dev/contributing/documenting_fastf1.html

Alternatively, the CI generates the documentation as a build artifact that you can download from the CI run summary. But this being your first pull request, I need to manually approve each CI run for every new change that you push. So this slows things down quite a bit.

Copy link
Contributor

@harningle harningle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need the filename to start with "plot_" for Sphinx-Gallery to run the file and get the graph. See https://sphinx-gallery.github.io/stable/configuration.html#parsing-and-executing-examples-via-matching-patterns

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To execute this file, the filename should start with "plot_"

fig.update_xaxes(range=[0, max_range_x],
showgrid=False,
showline=False)
###############################################################################
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We actually can stop right here. Sphinx-Gallery needs the last line to be a plotly Figure object, and fig.update_xaxes already returns a Figure obj. See https://sphinx-gallery.github.io/stable/configuration.html#parsing-and-executing-examples-via-matching-patterns.

If we add fig; show(fig) below, we will get a second/duplicate graph

import plotly.graph_objects as go
from plotly.io import show

# SETTING UP THE DATA #########################################################
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comments/explanation doesn't render. The expected format is:

##############################################################################
# Setting up the data
# Some other notes
print('python code here')

# Also, we have to define the number of laps for getting a nice x axis.
# We're picking 0 for start and last lap number + 1
howmany_laps = race.laps['LapNumber'].max()
max_range_x = howmany_laps + 1
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the notorious pandas SettingWithCopyWarning here... Suppress the warning or use a copy?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong line, I think, should be L47-48

# slowest lap of the quick laps and we're defining a margin of 0'5s for the
# limits of the graph.
quicklaps = race.laps.pick_quicklaps()
quicklaps['LapTime(s)'] = quicklaps['LapTime'].dt.total_seconds()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.pick_quicklaps returns a slice of the entire laps data. Therefore, setting quicklaps['LapTime(s)'] sets the new column of a copy of that slice which triggers the pandas warning.
Either, use race.laps.pick_quicklaps().copy() or just don't set the lap time in seconds as a new column. It could just be its own variable.
Please do not suppress the warning, that's just confusing as well.

@sierrapablo sierrapablo closed this by deleting the head repository Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants