我有属于三个不同类别的数据点。另一方面,我对其类别中的每个数据点都有权重。我想根据点的权重为我的点着色,但使用三种不同的连续颜色范围。实际上我想要类似下图的东西(这是手工制作的)。现在我使用 Plotly 进行着色,但欢迎任何其他与 python 兼容的方法。
实际上我想合并代码的两个输出:
if __name__ == '__main__':
n_data = 100
n_class = 3
t1 = [random.random() for i in range(n_data)]
t2 = [random.random() for i in range(n_data)]
class_color = [str(random.randint(1,n_class)) for i in range(n_data)]
weight_color = [random.random() for i in range(n_data)]
df = pd.DataFrame()
print(len(t1))
print(len(t2))
df['x'] = t1
df['y'] = t2
df['class_color'] = class_color
df['weight_color'] = weight_color
fig1 = px.scatter(df, x="x", y="y", color="class_color")
fig1.show()
fig2 = px.scatter(df, x="x", y="y", color="weight_color")
fig2.show()
最佳答案
请不要将其视为答案(暂时)。据我所知,您可以在绘图中使用不同的色标。但您应该研究如何正确显示所有图例
import plotly.graph_objects as go
import plotly.express as px
df = px.data.iris()
dfs = [d[1] for d in list(df.groupby('species'))]
fig = go.Figure()
fig.add_trace(
go.Scatter(x=dfs[0]["sepal_width"],
y=dfs[0]["sepal_length"],mode="markers",
marker=dict(color=dfs[0]["sepal_length"],
colorscale='Viridis',
showscale=True),
name=dfs[0]["species"].unique()[0],
showlegend=False
))
fig.add_trace(
go.Scatter(x=dfs[1]["sepal_width"],
y=dfs[1]["sepal_length"],mode="markers",
marker=dict(color=dfs[1]["sepal_length"],
colorscale='Magenta',
showscale=False),
name=dfs[1]["species"].unique()[0],
showlegend=False
))
fig.add_trace(
go.Scatter(x=dfs[2]["sepal_width"],
y=dfs[2]["sepal_length"],mode="markers",
marker=dict(color=dfs[2]["sepal_length"],
colorscale='Cividis',
showscale=False),
name=dfs[2]["species"].unique()[0],
showlegend=False
))
关于python - Python中不同类的不同连续颜色条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61169893/