Plotly:在 create_annotated_heatmap() 函数中定义 Z 参数的最佳方式
Plotly: best way to define the Z argument in the create_annotated_heatmap() function
我是 Plotly 和 Dash 的新手。我正在尝试创建一个显示底层数值的热图。
https://plotly.com/python/heatmaps/ 的文档说可以使用 ff.create_annotated_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()
第一个参数 data
似乎是一个列表列表。
我的数据如下:
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()
此代码有效,但当 Make
列中的值不是 "Ford"、"Buick" 时,它 会崩溃 或 "Mercedes"(或者如果元素数量增加或减少)。
如您所见,我在将 ford_scores
、buick_scores
和 mercedes_scores
传递给 create_annotated_heatmap() 函数中的 Z 参数之前手动定义它们.
这很老套。必须有更好的方法!
有没有办法将“df
”数据框传递给 Z 参数,使得函数 "understands" Z 参数由 'Score' 中的值组成柱子?如果没有,是否有另一种方法可以传递 Z 参数,这样就不需要预先了解数据和预处理列表? (也就是说,它对于传入的内容是不可知的和灵活的)
谢谢!
事实证明,有更好(而且更简单)的方法!由 Plotly 的朋友提供,解决方案如下:
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()
希望这对以后的人有所帮助!
我是 Plotly 和 Dash 的新手。我正在尝试创建一个显示底层数值的热图。
https://plotly.com/python/heatmaps/ 的文档说可以使用 ff.create_annotated_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()
第一个参数 data
似乎是一个列表列表。
我的数据如下:
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()
此代码有效,但当 Make
列中的值不是 "Ford"、"Buick" 时,它 会崩溃 或 "Mercedes"(或者如果元素数量增加或减少)。
如您所见,我在将 ford_scores
、buick_scores
和 mercedes_scores
传递给 create_annotated_heatmap() 函数中的 Z 参数之前手动定义它们.
这很老套。必须有更好的方法!
有没有办法将“df
”数据框传递给 Z 参数,使得函数 "understands" Z 参数由 'Score' 中的值组成柱子?如果没有,是否有另一种方法可以传递 Z 参数,这样就不需要预先了解数据和预处理列表? (也就是说,它对于传入的内容是不可知的和灵活的)
谢谢!
事实证明,有更好(而且更简单)的方法!由 Plotly 的朋友提供,解决方案如下:
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()
希望这对以后的人有所帮助!