风格Seaborn和绘声绘色的热图

2024-04-19 04:14:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我和Seaborn设计了一个情节:

# imports
import random

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

# 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
plt.figure(figsize=(20,20))
sns.heatmap(data = df,
            annot = True,
            vmin = 0.0,
            vmax = 1.0,
            cmap = 'PuBuGn')

enter image description here

下面是我如何创建情节图:

^{pr2}$

enter image description here

我有以下问题:

  • Seaborn:如何将y轴标签更改为YYYY-MM?在
  • 情节:如何将旧数据放在顶端(比如Seaborn)
  • 情节:如何获得热图中显示的数据标签(就像Seaborn那样)。在
  • 我注意到渲染比Seaborn慢得多。它能被优化吗?如果是,怎么做?在

Tags: importdfdatamatplotlibasnprangeplt
1条回答
网友
1楼 · 发布于 2024-04-19 04:14:17

2.如何将旧数据放在顶端?在

fig.update_yaxes(autorange="reversed")

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

^{pr2}$

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()

相关问题 更多 >