Cannot plotting a grid on scatter visualization

Hi
I am trying to plot a grid on scatter. I tried all the way but it doesn’t work for me. Could you please help me here. Below is my code.

import pandas as pd
import plotly.express as px
import matplotlib.pyplot as plt

workflow_nm=['worklfow_1','worklfow_2','worklfow_3','worklfow_4','worklfow_5']
date=['2024-02-09','2024-02-02','2024-02-07','2024-02-13','2024-02-28']
time=['10:00:00','14:00:00','07:00:00','20:00:00','16:00:00']

final_dict = {'dag_id': workflow_nm, 'date': date, 'time': time}
df = pd.DataFrame(final_dict)

final_df = df.sort_values('time')
fig =px.scatter(final_df, x='date', y='time', color="dag_id")



# Figure layout
fig.update_layout(template='simple_white',  width=700, height=1000, title='Worfklow Schedule Visualization', yaxis_title='Schedule Time',
                  legend=dict(title='TagName', itemclick='toggle', itemsizing='constant', traceorder='normal',
                  bgcolor='rgba(0,0,0,0)', x=1),
                  xaxis=dict(title='Schedule Date', showticklabels=True, ticks='outside', type='category'))

fig.update_xaxes(griddash='solid')
# Make figure zoomable
config = dict({'scrollZoom': False})

fig.write_html("visual_dag.html")
fig.show(config=config)

hi @rkumar
:wave: Welcome to the community.

You’re not seeing the grid lines because you chose template='simple_white'.

Try a different grid template and you’ll see the lines. For example, template='seaborn'.

I removed that line of code and went for: fig.update_xaxes(tickangle=-45, showgrid=True)


full code:

import pandas as pd
import plotly.express as px

# Initial data
workflow_nm = ['workflow_1', 'workflow_2', 'workflow_3', 'workflow_4', 'workflow_5']
date = ['2024-02-09', '2024-02-02', '2024-02-07', '2024-02-13', '2024-02-28']
time = ['10:00:00', '14:00:00', '07:00:00', '20:00:00', '16:00:00']

# Create DataFrame
final_dict = {'dag_id': workflow_nm, 'date': date, 'time': time}
df = pd.DataFrame(final_dict)

# Combine date and time into a single datetime column for proper sorting
df['datetime'] = pd.to_datetime(df['date'] + ' ' + df['time'])

# Sort by the new datetime column
final_df = df.sort_values('datetime')

# Create the scatter plot
fig = px.scatter(final_df, x='date', y='time', color="dag_id", title='Workflow Schedule Visualization')

# Customize the layout
fig.update_layout(template='simple_white', width=700, height=400,
                  legend=dict(title='TagName', itemclick='toggle', itemsizing='constant', traceorder='normal',
                              bgcolor='rgba(0,0,0,0)', x=1),
                  xaxis=dict(title='Schedule Date', showticklabels=True, ticks='outside', type='category'),
                  yaxis_title='Schedule Time')

fig.update_xaxes(tickangle=-45, showgrid=True)  # Rotate date labels for better readability show grid on xaxes
fig.update_yaxes(type='category')  # Treat time as categorical data for better visualization

# Make figure zoomable (Note: scrollZoom is a configuration option used with plotly.io.show, not with fig.show)
fig.show()

# Save the figure to an HTML file
fig.write_html("visual_dag.html")

change fig.update_yaxes(type='category') to: fig.update_yaxes(type='category', showgrid=True) for yaxes grid.

1 Like

Thanks.it is working for me