Hi all,
How to make line chart work with count method?
Code is following:
So what i need is Years on x axis, count on y axis and multiple lines each representing a genre.
One genre per line chart would be good too.
Hi all,
How to make line chart work with count method?
Code is following:
So what i need is Years on x axis, count on y axis and multiple lines each representing a genre.
One genre per line chart would be good too.
Hi! and Welcome to the community!
For what I could sense in your question, this is a kind of newbie question Sorry if you’re not and it’s OK if you are! A couple of tips to post new question, or at least what I think:
Try always to add your data, as long as you could or is not confidential (obviously). That is a good starting point to try to replicate the issue or whatever you’re trying, at least to me.
As I can see there, you’ve got a nice pandas
df
to play with, so try to play it to exhaustion and that’s the way your learn the most, for example if in your dataset you try after the group by
the option as_index = False
you will obtain not group on index will allow you to manipulate the data more fluently. Here you have two links to dive in:
as_index
option. Link-to-pandas-here!as_index
vs reset_index
option in Pandas! Great difference!! Look at it! Never forget to check to StackOverflow or just google for it and StackOverflow will pop-up and throw you an answer!! Bonus Point! In this AI time, try first ChatGPT (or whatever you like) and play with that, again: you will learn by doing it!! Check this prompt (and the answer!!)
A Plotly line-chart short question:
Suppose I need to plot several lines in the same line-chart from a dataset provided with the amount of movies by years classified by genre. On X axis I need to plot ‘Years’ and the Y axis represents the amount of movies. How would you that? Provide me the code with some toy data.
The answer (copy and past and you get your answer):
import plotly.express as px
import pandas as pd
# Create a toy dataset
data = {
'Year': [2010, 2011, 2012, 2013, 2014, 2015]*3,
'Genre': ['Action']*6 + ['Comedy']*6 + ['Drama']*6,
'Movies': [12, 15, 13, 17, 16, 20, # Action counts
7, 9, 8, 10, 11, 13, # Comedy counts
15, 18, 17, 20, 21, 19] # Drama counts
}
df = pd.DataFrame(data)
# Plotting multiple lines by genre
fig = px.line(df, x='Year', y='Movies', color='Genre', markers=True,
title='Movies per Year by Genre')
fig.show()
And the plot… Have a nice day! As someone in here would said: "We’re better together so help each other out!
Thanks a lot. I am newbie no doubt.
Dataset is from here https://datasets.imdbws.com/name.basics.tsv.gz
Blockquote
grouped = df.groupby([‘genres’,‘startYear’],as_index=False)
grouped.agg(‘count’)
df1 = grouped.agg(‘count’)
fig = px.line(df1, x=‘startYear’, y=‘tconst’, color = ‘genres’)
fig.show()
Blockquote
This looks like something i envisioned