Currently I’m trying to plot a scatter plot and a line plot in the same graph using plotly. Whenever I’m plotting them seperately, I can set a column as the color without any issues. However when I’m trying to add them together into one plot, errors occur. I’ve tried several approaches but none have helped me so far.
Hi @joziask , welcome to the forums.
What exactly are you trying to do?
Concerning the scatter plot, I guess something like this:
import plotly.express as px
import pandas as pd
import random
# create data
days=7
timespan = pd.date_range('01/01/2022', periods=days, freq='D')
value = [np.random.randint(1,50) for _ in range(days)]
color = [np.random.randint(1,50) for _ in range(days)]
# create figure
fig = px.scatter(x=timespan, y=value, color=color)
fig.show()
What about the line plot?
I’m trying to show a correlation between two variables from different datasets. One is the opening and closing of lock gates, and the other one is true or false value. As I’ve stated earlier, the code works fine when I’m plotting them seperately as shown below.
fig1 = px.line(deurhoogte1_week, x=‘timestamp’, y=‘value’, color=‘ODS_Omschrijving’)
fig1.show()
fig2 = px.scatter(toren1, x=‘timestamp’, y=‘value’)
fig2.show()
My question is how to get these two in one graph? While also maintaining the set column as a color.
Something like this?
import plotly.express as px
import pandas as pd
import random
# create data
days=7
timespan = pd.date_range('01/01/2022', periods=days, freq='D')
value = [np.random.randint(1,50) for _ in range(days)]
color = [np.random.randint(1,50) for _ in range(days)]
value2 = [np.random.randint(1,50) for _ in range(days)]
# create figure
fig = px.scatter(x=timespan, y=value, color=color)
fig.add_scatter(x=timespan, y=value2, mode='lines+markers', showlegend=False)
fig.show()
Try changing the size of the marker for the y2 values using the marker argument: marker={'size':30}
Must be something related to your data then. How do the figures look like if you plot them separately?
The line plot can be seen in earlier sent pictures, the scatter plot looks like this when plotted seperately:
Ahh, you have to convert True/False to a numerical value like so:
import plotly.express as px
import pandas as pd
import random
# create data
days=7
timespan = pd.date_range('01/01/2022', periods=days, freq='D')
value = [np.random.randint(1,50) for _ in range(days)]
color = [np.random.randint(1,50) for _ in range(days)]
value2 = [[True, False][np.random.randint(0,2)] for _ in range(days)]
value2 = [1 if val else 0 for val in value2]
# create figure
fig = px.scatter(x=timespan, y=value, color=color)
fig.add_scatter(x=timespan, y=value2, mode='markers+lines',marker={'size':30}, showlegend=False)
fig.show()
Thanks! I’ve got my markers showing up now. But I’m struggling with setting markers for every opening and closing of the lock gate as I’m intending. In my data, whenever the lock gates are fully opened or closed, another column (‘value’) will change to True or False. I’m trying to set these markers on the right spot. So I’m expecting markers on the bottom and top of the various cycles that my line plot show. Any ideas how to implement this?
Basically you are talking about the marker position on the y- axis right?
So what I would do is instead of
value2 = [1 if val else 0 for val in value2]
use appropiate values for the y-coordinate dependent on the other trace value at the same x-value (timestamp). Does this makes sense?
No not really, could you elaborate?
No, unfortunately I can’t since I obviously did not understand, what you want to do
What does your plot look like right now and how do you want it to be?
Haha no worries, so basically I want the scatter plot seen in the second image to overlap the lines in the first image. With true values being at the top of the lines and the false ones at the bottom of the lines
Like this? Keep in mind, that the True/False values in my example are random.
import plotly.express as px
import pandas as pd
import numpy as np
# set period (must be divisible by 6)
days=60
# x- axis data
timespan = pd.date_range('01/01/2022', periods=days, freq='D')
# set values for curve
value = [0,0,0,40,40,40] * (days//6)
# set True/False randomly
value2 = [[True, False][np.random.randint(0,2)] for _ in range(days)]
# convert True/False into numerical values
value2 = [val if val2 else 0 for val2, val in zip(value2, value)]
# create figure
fig = px.line(x=timespan, y=value)
fig.add_scatter(x=timespan, y=value2, mode='markers',marker={'size':7}, showlegend=False)
fig.show()