Python Plotly:在create_annotated_heatmap()函数中定义Z参数的最佳方法
我对Plotly和Dash是全新的。我正在尝试创建一个热图,以显示欠旋转的数值 位于的文档说明可以使用Python Plotly:在create_annotated_heatmap()函数中定义Z参数的最佳方法,python,plotly,plotly-dash,Python,Plotly,Plotly Dash,我对Plotly和Dash是全新的。我正在尝试创建一个热图,以显示欠旋转的数值 位于的文档说明可以使用ff.create\u annotated\u heatmap()函数,如下所示: import plotly.figure_factory as ff z = [[.1, .3, .5], [1.0, .8, .6], [.6, .4, .2]] x = ['Team A', 'Team B', 'Team C'] y = ['Game Three', 'Game Tw
ff.create\u annotated\u heatmap()
函数,如下所示:
import plotly.figure_factory as ff
z = [[.1, .3, .5],
[1.0, .8, .6],
[.6, .4, .2]]
x = ['Team A', 'Team B', 'Team C']
y = ['Game Three', 'Game Two', 'Game One']
z_text = [['Win', 'Lose', 'Win'],
['Lose', 'Lose', 'Win'],
['Win', 'Win', 'Lose']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
ford_scores = df[(df['Make'].isin(['Ford']))]['Score'].astype(float).tolist()
buick_scores = df[(df['Make'].isin(['Buick']))]['Score'].astype(float).tolist()
mercedes_scores = df[(df['Make'].isin(['Mercedes']))]['Score'].astype(float).tolist()
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=[ford_scores, buick_scores, mercedes_scores],
x=df['Dimension'].unique().tolist(),
y=df['Make'].unique().tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
df['Score'] = pd.to_numeric(df['Score'])
df = pd.pivot_table(df, values='Score', index='Make', columns=['Dimension'])
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=df.to_numpy(),
x=df.columns.tolist(),
y=df.index.tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
第一个参数,data
,似乎是一个列表列表
我的数据如下:
import plotly.figure_factory as ff
z = [[.1, .3, .5],
[1.0, .8, .6],
[.6, .4, .2]]
x = ['Team A', 'Team B', 'Team C']
y = ['Game Three', 'Game Two', 'Game One']
z_text = [['Win', 'Lose', 'Win'],
['Lose', 'Lose', 'Win'],
['Win', 'Win', 'Lose']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
ford_scores = df[(df['Make'].isin(['Ford']))]['Score'].astype(float).tolist()
buick_scores = df[(df['Make'].isin(['Buick']))]['Score'].astype(float).tolist()
mercedes_scores = df[(df['Make'].isin(['Mercedes']))]['Score'].astype(float).tolist()
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=[ford_scores, buick_scores, mercedes_scores],
x=df['Dimension'].unique().tolist(),
y=df['Make'].unique().tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
df['Score'] = pd.to_numeric(df['Score'])
df = pd.pivot_table(df, values='Score', index='Make', columns=['Dimension'])
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=df.to_numpy(),
x=df.columns.tolist(),
y=df.index.tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
我的代码如下:
import plotly.figure_factory as ff
z = [[.1, .3, .5],
[1.0, .8, .6],
[.6, .4, .2]]
x = ['Team A', 'Team B', 'Team C']
y = ['Game Three', 'Game Two', 'Game One']
z_text = [['Win', 'Lose', 'Win'],
['Lose', 'Lose', 'Win'],
['Win', 'Win', 'Lose']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
ford_scores = df[(df['Make'].isin(['Ford']))]['Score'].astype(float).tolist()
buick_scores = df[(df['Make'].isin(['Buick']))]['Score'].astype(float).tolist()
mercedes_scores = df[(df['Make'].isin(['Mercedes']))]['Score'].astype(float).tolist()
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=[ford_scores, buick_scores, mercedes_scores],
x=df['Dimension'].unique().tolist(),
y=df['Make'].unique().tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
df['Score'] = pd.to_numeric(df['Score'])
df = pd.pivot_table(df, values='Score', index='Make', columns=['Dimension'])
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=df.to_numpy(),
x=df.columns.tolist(),
y=df.index.tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
此代码有效,但当Make
列中的值不是“Ford”、“Buick”或“Mercedes”(或者如果元素数量增加或减少)时,它会发生惊人的故障
如您所见,我正在手动定义ford_scores
、buick_scores
和mercedes_scores
,然后将它们传递给create_annotated_heatmap()函数中的Z参数
这是哈奇。一定有更好的办法
是否有方法将“df
”数据框传递给Z参数,以便函数“理解”Z参数由“Score”列中的值组成?如果没有,是否有其他方法传递Z参数,这样做不需要预先了解数据和预处理列表?(也就是说,它对于传递的信息是不可知的和灵活的)
谢谢 事实证明,有一种更好(且不太老套)的方法!承蒙Plotly的朋友介绍,解决方案如下:
import plotly.figure_factory as ff
z = [[.1, .3, .5],
[1.0, .8, .6],
[.6, .4, .2]]
x = ['Team A', 'Team B', 'Team C']
y = ['Game Three', 'Game Two', 'Game One']
z_text = [['Win', 'Lose', 'Win'],
['Lose', 'Lose', 'Win'],
['Win', 'Win', 'Lose']]
fig = ff.create_annotated_heatmap(z, x=x, y=y, annotation_text=z_text, colorscale='Viridis')
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
ford_scores = df[(df['Make'].isin(['Ford']))]['Score'].astype(float).tolist()
buick_scores = df[(df['Make'].isin(['Buick']))]['Score'].astype(float).tolist()
mercedes_scores = df[(df['Make'].isin(['Mercedes']))]['Score'].astype(float).tolist()
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=[ford_scores, buick_scores, mercedes_scores],
x=df['Dimension'].unique().tolist(),
y=df['Make'].unique().tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
df = pd.DataFrame({'Make':['Ford', 'Ford', 'Ford', 'Buick', 'Buick', 'Buick', 'Mercedes', 'Mercedes', 'Mercedes'],
'Score':['88.6', '76.6', '86.2', '79.1', '86.8', '96.4', '97.3', '98.7', '98.5'],
'Dimension':['Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling', 'Speed', 'MPG', 'Styling'],
'Month':['Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19', 'Apr-19']})
df['Score'] = pd.to_numeric(df['Score'])
df = pd.pivot_table(df, values='Score', index='Make', columns=['Dimension'])
import plotly.figure_factory as ff
fig = ff.create_annotated_heatmap(
z=df.to_numpy(),
x=df.columns.tolist(),
y=df.index.tolist(),
colorscale=['red', 'orange', 'yellow', 'green'],
hoverongaps=False
)
fig.show()
希望这对将来的人有帮助