gpt4 book ai didi

python - 如何使用 plotly figure_factory 仅绘制散点图矩阵的下半部分?

转载 作者:行者123 更新时间:2023-12-05 04:44:31 24 4
gpt4 key购买 nike

我从 ploty figure_factory 看到这个例子

import plotly.graph_objects as go
import plotly.figure_factory as ff

import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(20, 4),
columns=['Column A', 'Column B', 'Column C', 'Column D'])

df['Fruit'] = pd.Series(['apple', 'apple', 'grape', 'apple', 'apple',
'grape', 'pear', 'pear', 'apple', 'pear',
'apple', 'apple', 'grape', 'apple', 'apple',
'grape', 'pear', 'pear', 'apple', 'pear'])


fig = ff.create_scatterplotmatrix(df, diag='histogram',index='Fruit',
height=800, width=800)
fig.show()

还有剧情: enter image description here

但我想知道是否可以只绘制下半部分,因为上半部分是相同的。这可以通过 lotly.graph_objects(感谢 showupperhalf=False :例如

import plotly.graph_objects as go
import pandas as pd

df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/iris-data.csv')
index_vals = df['class'].astype('category').cat.codes

fig = go.Figure(data=go.Splom(
dimensions=[dict(label='sepal length',
values=df['sepal length']),
dict(label='sepal width',
values=df['sepal width']),
dict(label='petal length',
values=df['petal length']),
dict(label='petal width',
values=df['petal width'])],
showupperhalf=False, # remove plots on diagonal
text=df['class'],
marker=dict(color=index_vals,
showscale=False, # colors encode categorical variables
line_color='white', line_width=0.5)
))


fig.update_layout(
title='Iris Data set',
width=600,
height=600,
)

fig.show()

结果是: enter image description here

可以和figure_factory一样吗?提前致谢

或者可以将散点图从 go.Splom 更改为直方图?或方框?

最佳答案

简答:

您可以通过将子图中的所有迹线替换为空的迹线对象(例如 go.Scatter()

)来清空您想要的任何子图

详情:

Scatterplot Matrix似乎没有对角线上直方图的选项。和 ff.create_scatterplotmatrix似乎没有隐藏部分矩阵的选项。但这并不意味着您在这里尝试做的事情是不可能的。

下面完整的代码片段使用了一个矩阵...

shows = array([[1., 0., 0., 0.],
[1., 1., 0., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 1.]])

...决定是否应显示散点图矩阵的每个子图的数据。如果 value = 0,则子图中的特定轨迹...

Histogram({
'marker': {'color': 'rgb(31, 119, 180)'},
'showlegend': False,
'x': [-1.4052399956918005, -1.8538677019305498, -0.0016298185761457061,
-0.5268239747464603, -0.10652357762295094, 0.02921346151566477,
-0.4581921321144815, 0.020997665069043978, 0.8734380864952216,
-1.3008441288553083],
'xaxis': 'x',
'yaxis': 'y'
})

... 简单地替换为:

go.Scatter()

如果子图中的所有轨迹都发生这种情况,那么该图将被空白,就像您在此处看到的那样:

plotly :

enter image description here

完整代码:

import plotly.graph_objects as go
import plotly.figure_factory as ff
import numpy as np
import pandas as pd
import plotly.express as px

cols = ['Column A', 'Column B', 'Column C', 'Column D']
# cols = ['Column A', 'Column B', 'Column C']
# cols = ['Column A', 'Column B', 'Column C', 'Column D', 'Column E', 'Column F']
df = pd.DataFrame(np.random.randn(20, len(cols)),
columns=cols)

df['Fruit'] = pd.Series(['apple', 'apple', 'grape', 'apple', 'apple',
'grape', 'pear', 'pear', 'apple', 'pear',
'apple', 'apple', 'grape', 'apple', 'apple',
'grape', 'pear', 'pear', 'apple', 'pear'])

fig = ff.create_scatterplotmatrix(df, diag='histogram', index='Fruit',
height=800, width=800)


# get number of rows and columns
ix = df['Fruit'].unique()
xs = []
ys = []
fig.for_each_xaxis(lambda x: xs.append(x.title.text) if x.title.text != None else ())
fig.for_each_yaxis(lambda y: ys.append(y.title.text) if y.title.text != None else ())

# build matrix to determine visiblity of trqces
m = (len(xs), len(ys))
show = np.tril(np.ones(m))
newshows = []
newdata = []
for i in range(0, len(show[0])):
lst = list(np.repeat(show[i], len(ix)))
newshows.extend(lst)

# replace existing data with empty data in the upper triangle
for t, s in enumerate(newshows):
if newshows[t] == 0:
newdata.append(go.Scatter())
else:
newdata.append(fig.data[t])

fig.data = []
for i, new in enumerate(newdata):
fig.add_trace(newdata[i])

fig.show()

关于python - 如何使用 plotly figure_factory 仅绘制散点图矩阵的下半部分?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69356669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com