欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python画热图之seaborn

程序员文章站 2022-04-19 12:42:22
import matplotlib.pylab as pltimport seaborn as snsimport numpy as npfrom scipy.stats import kdemy_dpi=96plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)x = np.random.normal(size=500)y = x * 3 + np.random.normal(size=500)nbins=300k = kd....
import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
from scipy.stats import kde

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)

nbins=300
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
plt.pcolormesh(xi, yi, zi.reshape(xi.shape))
plt.show()

Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
from scipy.stats import kde

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)

nbins=300
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.Greens_r)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
from scipy.stats import kde

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)
x = np.random.normal(size=500)
y = x * 3 + np.random.normal(size=500)

nbins=300
k = kde.gaussian_kde([x,y])
xi, yi = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
zi = k(np.vstack([xi.flatten(), yi.flatten()]))
plt.pcolormesh(xi, yi, zi.reshape(xi.shape), cmap=plt.cm.Greens_r)
plt.colorbar()
plt.show()

 Python画热图之seaborn

 

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((5,5)), columns=["a","b","c","d","e"])
p1 = sns.heatmap(df)
plt.show()

Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((100,5)), columns=["a","b","c","d","e"])
# 计算每对变量之间的相关性
corr_matrix=df.corr()
p1 = sns.heatmap(corr_matrix, cmap='PuOr')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((100,5)), columns=["a","b","c","d","e"])
# 计算每对变量之间的相关性
corr_matrix=df.corr()
# 画半个矩阵
mask = np.zeros_like(corr_matrix)
mask[np.triu_indices_from(mask)] = True
with sns.axes_style("white"):
    p2 = sns.heatmap(corr_matrix, mask=mask, square=True)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

people=np.repeat(("A","B","C","D","E"),5)
feature=list(range(1,6))*5
value=np.random.random(25)
df=pd.DataFrame({'feature': feature, 'people': people, 'value': value })
df_wide=df.pivot_table( index='people', columns='feature', values='value' )
p2=sns.heatmap( df_wide )
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p1 = sns.heatmap(df, linewidths=2, linecolor='yellow')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p2 = sns.heatmap(df, annot=True, annot_kws={"size": 7})
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p3 = sns.heatmap(df, cbar=False)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p4 = sns.heatmap(df, yticklabels=False)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p5 = sns.heatmap(df, xticklabels=4)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p1 = sns.heatmap(df, cmap="YlGnBu")
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p1 = sns.heatmap(df, cmap="Blues")
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p1 = sns.heatmap(df, vmin=0, vmax=0.5)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.random((10,10)), columns=["a","b","c","d","e","f","g","h","i","j"])
p1 = sns.heatmap(df, vmin=0.5, vmax=0.7)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.randn(6, 6))

# 离散分布
df_q = pd.DataFrame()
for col in df:
    df_q[col] = pd.to_numeric( pd.qcut(df[col], 3, labels=list(range(3))) )

p1 = sns.heatmap(df_q)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = np.random.randn(30, 30)
p1 = sns.heatmap(df, cmap="PiYG")
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = np.random.randn(30, 30)
p1 = sns.heatmap(df, center=1)
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(10,10) * 4 + 3)
df[1]=df[1]+40
p1 = sns.heatmap(df, cmap='viridis')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd

df = pd.DataFrame(np.random.randn(10,10) * 4 + 3)
df[1]=df[1]+40

df_norm_col=(df-df.mean())/df.std()
p2 = sns.heatmap(df_norm_col, cmap='viridis')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.randn(10,10) * 4 + 3)
df.iloc[2]=df.iloc[2]+40

p3 = sns.heatmap(df, cmap='viridis')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
np.random.seed(0)

df = pd.DataFrame(np.random.randn(10,10) * 4 + 3)
df.iloc[2]=df.iloc[2]+40
# 减去均值
df_norm_row=df.sub(df.mean(axis=1), axis=0)

df_norm_row=df_norm_row.div( df.std(axis=1), axis=0 )

p4 = sns.heatmap(df_norm_row, cmap='viridis')
plt.show()

 Python画热图之seaborn

import matplotlib.pylab as plt
import seaborn as sns
import numpy as np
import pandas as pd
from mpl_toolkits.mplot3d import Axes3D

my_dpi=96
plt.figure(figsize=(480/my_dpi, 480/my_dpi), dpi=my_dpi)

# 获得网页数据
url = 'https://python-graph-gallery.com/wp-content/uploads/volcano.csv'
data = pd.read_csv(url)

sns.heatmap(data, cmap="viridis")
# 坐标轴和标题
plt.tick_params(labelbottom='off', labelleft='off')
plt.xlabel('Latitude')
plt.ylabel('Longitude')
plt.title('Altitude on the volcano area', loc='left' )
plt.show()

 Python画热图之seaborn

本博主新开公众号, 希望大家能扫码关注一下,十分感谢大家。

Python画热图之seaborn 

本文来自:https://github.com/holtzy/The-Python-Graph-Gallery/blob/master/PGG_notebook.py  

本文地址:https://blog.csdn.net/weixin_41869644/article/details/107160919