Black Lives Matter. Please consider donating to Black Girls Code today.

Plotly Line Charts For only for specific values of a column

So I am studying the global terrorism dataset that can be found on kaggle: https://www.kaggle.com/ash316/terrorism-around-the-world and I would like to make a line charts of the top 10 terrorist groups based on their activity (“Group” column) with the “Year” column on the x-axis.

How can I do just that ? I tried:

fig = px.line(df, x=df.Year, color=df.Group.value_counts().nlargest(10).index, width=800, height=800)

fig.update_layout(legend_orientation="h")

fig.show()

But that gives me the error:

ValueError: All arguments should have the same length. The length of argument \color` is 10, whereas the length of previous arguments ['Year'] is 170350`

It works fine when I do it in this specific way but since there are just way too many terrorist groups, this results in a very ugly visualization and I must make the legend orientation horizontal… and I end up with a super long legend…

fig = px.line(df, x=df.Year, color=df.Group, width=800, height=800)

fig.update_layout(legend_orientation="h")

fig.show()

Hi @Plotlyisprettykewl welcome to the forum! The best thing would probably to filter your dataset before creating the figure, otherwise you will load the whole dataset into the browser and the global terrorism dataset is a large dataset… You could get the values of the groups you interested in, then query the dataset. For example below on the iris dataset from plotly.express:

import plotly.express as px
df = px.data.iris()
df.query("species in ['setosa', 'virginica']")