I am looking to plot a scatterplot given 4 columns, all in the format of tuple pairs, which will access a multiindex dataframe. This is the shortened form of the code, to spare several pages of code, but I have tried flattening the columns to strings to no avail.
Here is the data snippet:
<code>import plotly.express as px
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
columns = pd.MultiIndex.from_tuples([
("Q", "stuff"),
("R", "mostuff"),
("S", "othstuff"),
("T", "finalstuff"),
])
# Generate random data for the DataFrame
data = {
("Q", "stuff"): np.random.rand(100),
("R", "mostuff"): np.random.rand(100),
("S", "othstuff"): np.random.choice(['A', 'B', 'C', 'D'], 100),
("T", "finalstuff"): np.random.rand(100) * 100, # Ensure numeric data
}
df = pd.DataFrame(data, columns=columns)
</code>
<code>import plotly.express as px
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
columns = pd.MultiIndex.from_tuples([
("Q", "stuff"),
("R", "mostuff"),
("S", "othstuff"),
("T", "finalstuff"),
])
# Generate random data for the DataFrame
data = {
("Q", "stuff"): np.random.rand(100),
("R", "mostuff"): np.random.rand(100),
("S", "othstuff"): np.random.choice(['A', 'B', 'C', 'D'], 100),
("T", "finalstuff"): np.random.rand(100) * 100, # Ensure numeric data
}
df = pd.DataFrame(data, columns=columns)
</code>
import plotly.express as px
import numpy as np
from plotly.subplots import make_subplots
import plotly.graph_objects as go
import pandas as pd
columns = pd.MultiIndex.from_tuples([
("Q", "stuff"),
("R", "mostuff"),
("S", "othstuff"),
("T", "finalstuff"),
])
# Generate random data for the DataFrame
data = {
("Q", "stuff"): np.random.rand(100),
("R", "mostuff"): np.random.rand(100),
("S", "othstuff"): np.random.choice(['A', 'B', 'C', 'D'], 100),
("T", "finalstuff"): np.random.rand(100) * 100, # Ensure numeric data
}
df = pd.DataFrame(data, columns=columns)
This is then put in to a class and used in a panel, but the short form of the code is printed here. It assumes these are input as variables.
<code>def create_scatter(self, tupz=lambda _ls: tuple(map(str, _ls))):
_df = self.df.copy()
_df.columns = tupz(_df.columns.to_list())
_x, _y, _cl, _sz = tupz([self.x, self.y, self.color_col, self.size_col])
_df_sparse = _df.loc[:, [_x, _y, _cl, _sz]].dropna()
fig = px.scatter(
_df_sparse,
x=_x,
y=_y,
color=_cl,
size=_sz,
)
</code>
<code>def create_scatter(self, tupz=lambda _ls: tuple(map(str, _ls))):
_df = self.df.copy()
_df.columns = tupz(_df.columns.to_list())
_x, _y, _cl, _sz = tupz([self.x, self.y, self.color_col, self.size_col])
_df_sparse = _df.loc[:, [_x, _y, _cl, _sz]].dropna()
fig = px.scatter(
_df_sparse,
x=_x,
y=_y,
color=_cl,
size=_sz,
)
</code>
def create_scatter(self, tupz=lambda _ls: tuple(map(str, _ls))):
_df = self.df.copy()
_df.columns = tupz(_df.columns.to_list())
_x, _y, _cl, _sz = tupz([self.x, self.y, self.color_col, self.size_col])
_df_sparse = _df.loc[:, [_x, _y, _cl, _sz]].dropna()
fig = px.scatter(
_df_sparse,
x=_x,
y=_y,
color=_cl,
size=_sz,
)