WillH
WillH

Reputation: 303

Plotly Subplots using data to specify trace colours

I have a dataframe of vertical profiles of data and I want to plot two variables (Velocity & Temperature) against depth in two adjacent subplots, i.e. Row1/Col1 Depth v Velocity and Row1/Col2 Depth v Temperature.

I want all vertical profile (for each variable) to plot on the same graph and be colour coded by which profile they are i.e Dip1, Dip2. I don't need to control the colour, simply that they are differently coloured. The code below does what I want as individual figures, but how do I get them side by side on the same figure?

This is what I would like to create, you can see different Dips are in different colours. What I want

The code below creates the two separate web-browser images I used to create the image above.

import pandas as pd
import plotly.express as px

data= pd.read_csv('TS_DIP.000',delimiter='\t') #tab delimiter
df=pd.DataFrame(data, columns=['Depth','Temperature','Velocity','Dip No.'])
df['Depth']*=-1 # invert depths - below seabed

fig1 = px.line(df, x="Velocity", y="Depth", color="Dip No.",
                  title="Velocity Profiles")
fig2 = px.line(df, x="Temperature", y="Depth", color="Dip No.",
                  title="Temperature Profiles")
fig1.update_layout(xaxis_title='Velocity m/s',yaxis_title='Depth m',template="plotly_dark")
fig2.update_layout(xaxis_title='Temperature C',yaxis_title='Depth m',template="plotly_dark")

fig1.show()
fig2.show()

Fig 1 from code above

This is an example of the data from where we change from Dip1 to Dip2

Date    Velocity    Depth   Temperature Dip No.
26/04/2021  1478.824    2.669   7.055   Dip1
26/04/2021  1478.811    1.842   7.053   Dip1
26/04/2021  1478.809    1.366   7.052   Dip1
26/04/2021  1478.810    1.678   7.054   Dip1
26/04/2021  1478.810    2.118   7.052   Dip1
26/04/2021  1478.814    2.263   7.050   Dip1
26/04/2021  1478.813    2.166   7.051   Dip1
26/04/2021  1478.809    1.952   7.052   Dip1
26/04/2021  1478.797    1.811   7.052   Dip1
25/04/2021  1479.753    2.872   7.317   Dip2
25/04/2021  1479.751    2.996   7.312   Dip2
25/04/2021  1479.762    3.531   7.315   Dip2
25/04/2021  1479.774    4.123   7.312   Dip2
25/04/2021  1479.807    4.645   7.315   Dip2
25/04/2021  1479.818    4.987   7.317   Dip2
25/04/2021  1479.827    5.230   7.318   Dip2
25/04/2021  1479.826    5.427   7.320   Dip2
25/04/2021  1479.834    5.632   7.322   Dip2
25/04/2021  1479.843    6.128   7.321   Dip2
25/04/2021  1479.859    6.966   7.324   Dip2
25/04/2021  1479.870    7.957   7.323   Dip2

I have looked at fig=make_subplots(rows=1,cols=2) but the examples I have seen then use variations of fig.add_trace(go.Scatter(x=df['Velocity'], y=df['Depth']),row=1, col=1) and I don't seem to be able to find the correct way of duplicating the color="Dip No." command that I have in the code above, so all Dips are the same colour.

Upvotes: 0

Views: 575

Answers (2)

WillH
WillH

Reputation: 303

Based on the above answers and comments, I have edited the code to include a method of adding traces automatically based on the number of non-unique events in the Dip column.

import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots
import pandas as pd

data= pd.read_csv('/Users/Test-data/TS-DIP.000',delimiter='\t') #tab delimiter
df=pd.DataFrame(data, columns=['Depth','Temperature','Velocity','Dip'])
df['Depth']*=-1 # invert depths - below seabed

dips=df['Dips'].nunique() #Count the number of dip by counting unique numbers


fig1 = px.line(df, x='Velocity', y='Depth', color='Dip')
fig2 = px.line(df, x='Temperature', y='Depth', color='Dip')


fig = make_subplots(rows=1, cols=2,
                    column_titles = ['Velocity m/s','Temperature C'],
                    row_titles = ['Depth m','Depth m'],
                    shared_yaxes=True)
for d in range (dips):
    fig.add_trace(fig1['data'][d], row=1, col=1)

for d in range (dips):
    fig.add_trace(fig2['data'][d], row=1, col=2)

fig.update_layout(template="plotly_dark")

fig.show()

Upvotes: 0

r-beginners
r-beginners

Reputation: 35115

I reviewed it in response to your comment. It is simple to use PX (EXPRESS) to simplify the color coding, so I used it to construct the subplot. There is a duplicate legend and I am looking into ways to eliminate that, does this fit your intent?

import plotly.graph_objects as go
import plotly.express as px
from plotly.subplots import make_subplots

fig1 = px.line(df, x='Velocity', y='Depth', color='Dip No.')
fig2 = px.line(df, x='Temperature', y='Depth', color='Dip No.')

fig = make_subplots(rows=1, cols=2,
                    column_titles = ['Velocity m/s','Temperature C'],
                    row_titles = ['Depth m','Depth m'],
                    shared_yaxes=False)

fig.add_trace(fig1['data'][0], row=1, col=1)
fig.add_trace(fig1['data'][1], row=1, col=1)

fig.add_trace(fig2['data'][0], row=1, col=2)
fig.add_trace(fig2['data'][1], row=1, col=2)

fig.update_layout(template="plotly_dark")

fig.show()

enter image description here

Upvotes: 1

Related Questions