Plotly - How to add count of rows to Y axis on line chart?

hockeyhorror

I'm trying to create a simple line chart with the date field on the X axis and the count of rows on the Y. I'm using the following code:

import plotly.express as px

data = {'Project':  ['Project 1', 'Project 2', ' Project 3', 'Project 4', 'Project 5', 'Project 6', 'Project 7', 'Project 8', 'Project 9', 'Project 10'],
        'Date': ['10/1/2020', '10/1/2020', '11/1/2020', '12/1/2020', '12/1/2020', '12/1/2020', '2/1/2021', '2/1/2021', '3/1/2021', '4/1/2021']}

df2 = pd.DataFrame(data, columns = ['Project','Date'])

fig = px.line(df2, x= "Date", y = "Project", title='<b>Project</b>')
fig.show()

But when I do that the project names are on the X axis instead of the count of projects for each date.

Does anyone know how I can add a count of rows so it shows the number of projects for each month of Date on the line chart?

RichieV

You need to groupby and count the rows in pandas before sending to plotly. Also, your sample does not show it, but if you expect different dates within the same month and you only care for the year/month then you need to apply some rounding before grouping (or extract the year & month from the date with data['Date'].dt.year and data['Date'].dt.month, whichever you prefer).

Take this slightly different sample with 10/2 thrown in there

import plotly.express as px

data = {'Project':  ['Project 1', 'Project 2', ' Project 3', 'Project 4', 'Project 5', 'Project 6', 'Project 7', 'Project 8', 'Project 9', 'Project 10'],
        'Date': ['10/1/2020', '10/2/2020', '11/1/2020', '12/1/2020', '12/2/2020', '12/1/2020', '2/1/2021', '2/1/2021', '3/1/2021', '4/1/2021']}

df2 = pd.DataFrame(data, columns = ['Project','Date'])
df2['Date'] = pd.to_datetime(df2['Date'])

df_grouped = (
    df2.groupby(
        # normalize all dates to start of month
        df2['Date'].astype('datetime64[M]')
    )['Project'].count().rename('Count').to_frame()
)
df_grouped['Names'] = (
    df2.groupby(df2['Date'].astype('datetime64[M]')
    )['Project'].agg(',<br>    '.join)
)

print(df_grouped)

fig = px.line(
    df_grouped, y='Count', title='<b>Projects per month</b>',
    hover_data=['Names']
)
fig.write_html('fig1.html', auto_open=True)

Update: as requested, this new code shows the project names on hover.

Output

            Count                                          Names
Date
2020-10-01      2                    Project 1,<br>    Project 2
2020-11-01      1                                      Project 3
2020-12-01      3  Project 4,<br>    Project 5,<br>    Project 6
2021-02-01      2                    Project 7,<br>    Project 8
2021-03-01      1                                      Project 9
2021-04-01      1                                     Project 10

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plotly: How to add annotations to different intervals of the y-axis?

Plotly: How to plot a line chart from two rows of data?

How to hide the y axis and x axis line and label in my bar chart for chart.js

How can I add a vertical line on the X axis in a Google Chart' chart (Line type)?

Plotly: How to make more space for y axis labels for gantt chart

R - creating a bar and line on same chart, how to add a second y axis

Plotly: Add line to bar chart

How can I add names/title/values (months) to Y axis for my line chart?

How to add points to line chart using plotly express?

Lines and secondary y-axis are not displayed in plotly barplot with overlying line chart

How to have second y-axis and have line chart embedded in it

how to add a second x-axis to a line chart in R

How to adjust the Morris line chart y axis dynamically

echarts add padding between line chart and y-axis label

How to make stacked line chart with different y-axis in matplotlib?

How to vanish X and Y axis line from plotly graph

Bar chart and vertical line with discrete axis in plotly

How to plot line curve with one x axis and two y axis with plotly?

How to make a bar chart on range of values on x-axis and count on the range on y-axis in python?

How to add 95% confidence interval for a line chart in Plotly?

How to add a line to a plotly express bar chart

Is there a way to add text to the `y2` axis of a plotly chart in R?

Python Plotly Line Chart with a Dataframe: Wrong scale on the y-axis

Add x-axis to matplotlib with multiple y-axis line chart

Chart.Js- y-axis line is misplaced on line chart

how to add reactive x and y axis labels to shiny plotly graph?

How to create Stacked Line Chart in chartjs Multiple Y Axis and common X Axis

How can I add 2 100% stacked bars (Y Axis) to each element in the X Axis of my chart in plotly?

Python Plotly barchart - how do I add a horizontal line on the chart?