定制 plotly create_scattermatrix 图
customization of plotly create_scattermatrix plots
简单调用 plotly 的 figure_factory 例程来创建散点矩阵:
import pandas as pd
import numpy as np
from plotly import figure_factory
df = pd.DataFrame(np.random.randn(40,3))
fig = figure_factory.create_scatterplotmatrix(df, diag='histogram')
fig.show()
产量
我的问题是:
- 如何为所有绘图指定一种颜色?
- 如何为散点图上的三个变量分别设置坐标轴范围?
- 有没有办法创建直方图的密度(归一化)版本?
- 有没有办法在非对角线图的右上角包含相关系数(例如,从
df.corr()
计算)?
第一次改成相同颜色,更新生成的图表数据中的marker属性颜色;修改第二个散点图的轴范围,以相同的方式更新生成的数据;由于只有 x-axis 被修改,如有必要,对 y-axis 使用相同的技术;更改为第三个直方图的规范化版本 要更改为第三个直方图的规范化版本,请将其替换为规范化数据。要替换的数据是参考文献中示例规范中完成的数据。如果这没有达到归一化,我相信可以用 np.histogram() 等获得的数据替换它。第四个是注释,但我已经添加了 df.corr() 获得的数据使用图形数据参考,按每个子图的轴名称指定数据。
import pandas as pd
import numpy as np
from plotly import figure_factory
np.random.seed(20220529)
df = pd.DataFrame(np.random.randn(40,3))
density = px.histogram(df, x=[0,1,2], histnorm='probability density')
df_corr = df.corr()
fig = figure_factory.create_scatterplotmatrix(df, diag='histogram', height=600, width=600)
# 1.How can I specify a single color for all the plots?
for i in range(9):
fig.data[i]['marker']['color'] = 'blue'
# 2.How can I set the axes ranges for each of the three variables on the scatter plot?
for axes in ['xaxis2','xaxis3','xaxis4','xaxis6','xaxis7']:
fig.layout[axes]['range']=(-4,4)
# 3.Is there a way to create a density (normalized) version of the histogram?
fig['data'][0]['histnorm'] = 'probability density'
fig['data'][4]['histnorm'] = 'probability density'
fig['data'][8]['histnorm'] = 'probability density'
# 4.Is there a way to include the correlation coefficient (say, computed from df.corr())
# in the upper right corner of the non-diagonal plots?
for r,x,y in zip(df_corr.values.flatten(),
['x1','x2','x3','x4','x5','x6','x7','x8','x9'],
['y1','y2','y3','y4','y5','y6','y7','y8','y9']):
if r == 1.0:
pass
else:
fig.add_annotation(x=3.3, y=2, xref=x, yref=y, showarrow=False, text='R:'+str(round(r,2)))
fig.show()
简单调用 plotly 的 figure_factory 例程来创建散点矩阵:
import pandas as pd
import numpy as np
from plotly import figure_factory
df = pd.DataFrame(np.random.randn(40,3))
fig = figure_factory.create_scatterplotmatrix(df, diag='histogram')
fig.show()
产量
我的问题是:
- 如何为所有绘图指定一种颜色?
- 如何为散点图上的三个变量分别设置坐标轴范围?
- 有没有办法创建直方图的密度(归一化)版本?
- 有没有办法在非对角线图的右上角包含相关系数(例如,从
df.corr()
计算)?
第一次改成相同颜色,更新生成的图表数据中的marker属性颜色;修改第二个散点图的轴范围,以相同的方式更新生成的数据;由于只有 x-axis 被修改,如有必要,对 y-axis 使用相同的技术;更改为第三个直方图的规范化版本 要更改为第三个直方图的规范化版本,请将其替换为规范化数据。要替换的数据是参考文献中示例规范中完成的数据。如果这没有达到归一化,我相信可以用 np.histogram() 等获得的数据替换它。第四个是注释,但我已经添加了 df.corr() 获得的数据使用图形数据参考,按每个子图的轴名称指定数据。
import pandas as pd
import numpy as np
from plotly import figure_factory
np.random.seed(20220529)
df = pd.DataFrame(np.random.randn(40,3))
density = px.histogram(df, x=[0,1,2], histnorm='probability density')
df_corr = df.corr()
fig = figure_factory.create_scatterplotmatrix(df, diag='histogram', height=600, width=600)
# 1.How can I specify a single color for all the plots?
for i in range(9):
fig.data[i]['marker']['color'] = 'blue'
# 2.How can I set the axes ranges for each of the three variables on the scatter plot?
for axes in ['xaxis2','xaxis3','xaxis4','xaxis6','xaxis7']:
fig.layout[axes]['range']=(-4,4)
# 3.Is there a way to create a density (normalized) version of the histogram?
fig['data'][0]['histnorm'] = 'probability density'
fig['data'][4]['histnorm'] = 'probability density'
fig['data'][8]['histnorm'] = 'probability density'
# 4.Is there a way to include the correlation coefficient (say, computed from df.corr())
# in the upper right corner of the non-diagonal plots?
for r,x,y in zip(df_corr.values.flatten(),
['x1','x2','x3','x4','x5','x6','x7','x8','x9'],
['y1','y2','y3','y4','y5','y6','y7','y8','y9']):
if r == 1.0:
pass
else:
fig.add_annotation(x=3.3, y=2, xref=x, yref=y, showarrow=False, text='R:'+str(round(r,2)))
fig.show()