样式Seaborn和Plotly热图

时间:2018-12-26 21:26:01

标签: python python-3.x pandas plotly seaborn

我与Seaborn创建了一个情节:

# create some random data
N = 20
rand_matrix = np.asarray([random.randrange(1,11)/10 for _ in range(1, N*N+1) ]).reshape(N,N)
data = np.flip(np.triu(rand_matrix), 1)
df = pd.DataFrame(data, index=pd.date_range(start='2015-01-01', freq='MS',\ 
                  periods=N), columns = range(1,N+1))
df[1]=1

# seaborn plot
%matplotlib inline
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

plt.figure(figsize=(20,20))
sns.heatmap(data = df,
            annot = True,
            vmin = 0.0,
            vmax = 1.0,
            cmap = 'PuBuGn')

enter image description here

这是创建Plotly图的方式:

import plotly.offline as py
import plotly.graph_objs as go

py.init_notebook_mode(connected=True)

data = [
    go.Heatmap(
        z=df.values,
        x=df.columns.tolist(),
        y=df.index.tolist()
    )
]

layout = go.Layout(
    xaxis = dict(ticks='', nticks=N),
    yaxis = dict(ticks='', nticks=N))

fig = go.Figure(data=data, layout=layout)
py.iplot(fig)

enter image description here

我有以下问题:

  • Seaborn:如何将y轴标签更改为YYYY-MM?
  • 密谋:如何将较早的数据推到顶部(就像使用Seaborn一样)
  • 密谋:如何获取显示在热图中的数据标签(就像Seaborn一样)。
  • 情节:我注意到渲染比Seaborn慢得多。如果可以的话,可以优化吗?

1 个答案:

答案 0 :(得分:1)

2。。如何将较早的数据显示在顶部?

fig.update_yaxes(autorange="reversed")

3。。如何获得显示在热图中的数据标签?

ff.create_annotated_heatmap()

4。是否可以进行总体优化?

这里没有快速修复方法。

情节:

enter image description here

密谋:

from plotly.offline import iplot
import plotly.graph_objs as go
import plotly.figure_factory as ff

#py.init_notebook_mode(connected=True)

data = [
    go.Heatmap(
        z=df.values,
        x=df.columns.tolist(),
        y=df.index.tolist()
    )
]

font_colors = ['white', 'black']
fig = ff.create_annotated_heatmap(z=df.values, colorscale='Jet', font_colors=font_colors)

fig.update_layout( yaxis = dict(ticks='', nticks=N))

# Make text size smaller
for i in range(len(fig.layout.annotations)):
    fig.layout.annotations[i].font.size = 8

# reverse scales
fig.update_yaxes(autorange="reversed")

fig.show()