编程语言
首页 > 编程语言> > Python | Matplotlib | 可视化合集Part2

Python | Matplotlib | 可视化合集Part2

作者:互联网

1 绘制密度图

1.1 密度图

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import gaussian_kde

# 创建数据
data = [1.2]*8 + [2.2]*5 + [3.6]*8 + [4.9]*5 + [5.3]*2 + [6.6]*6

# 创建一个密度函数,从X轴给这个函数一个值时,它在Y轴上返回相应的值
density = gaussian_kde(data)
density.covariance_factor = lambda : .25
density._compute_covariance()

# 创建数集并绘图
data2 = np.linspace(0, 9, 300)
plt.plot(data2,density(data2))
plt.show()

OUTPUT:

 1.2  二维密度图

import matplotlib.pyplot as plt
import numpy as np
from scipy.stats import kde
 
# 创建数据
x = np.random.normal(size=800)
y = x * 3 + np.random.normal(size=800)
nbins=500
k = kde.gaussian_kde([x,y])
x1, y1 = np.mgrid[x.min():x.max():nbins*1j, y.min():y.max():nbins*1j]
z1 = k(np.vstack([x1.flatten(), y1.flatten()]))
 
# 绘制
plt.pcolormesh(x1, y1, z1.reshape(x1.shape), shading='auto')
plt.show()

OUTPUT:

 2 绘制雷达图

2.1 单组雷达图


import matplotlib.pyplot as plt
import pandas as pd
from math import pi
 
# 创建数据
df = pd.DataFrame({
'group': ['A','B','C','D'],
'wri': [18, 5, 28, 9],
'ora': [23, 10, 9, 20],
'com': [18, 36, 27, 29],
'org': [7, 32, 35, 16],
'lea': [26, 15, 32, 18]
})

cate=list(df)[1:]
N = len(cate)
values=df.loc[0].drop('group').values.flatten().tolist()
values += values[:1]
values
 
# 雷达图的角度设置
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
 
 
# 绘图及各参数设置
ax = plt.subplot(111, polar=True)
plt.xticks(angles[:-1], cate, color='grey', size=15)
ax.set_rlabel_position(0)
plt.yticks([10,20,30], ["10","20","30"], color="grey", size=8)
plt.ylim(0,40)
ax.plot(angles, values, linewidth=1, linestyle='solid')
ax.fill(angles, values, 'b', alpha=0.1)
plt.show()

OUTPUT:

2.2 多组雷达图 

import matplotlib.pyplot as plt
import pandas as pd
from math import pi
 
# 创建数据
df = pd.DataFrame({
'group': ['A','B','C','D'],
'wri': [18, 5, 28, 9],
'ora': [23, 10, 9, 20],
'com': [18, 36, 27, 29],
'org': [7, 32, 35, 16],
'lea': [26, 15, 32, 18]
})

cate=list(df)[1:]
N = len(cate)
values=df.loc[0].drop('group').values.flatten().tolist()
values += values[:1]
values
 
# 雷达图的角度设置及图初始化
angles = [n / float(N) * 2 * pi for n in range(N)]
angles += angles[:1]
ax = plt.subplot(111, polar=True)

# 将一组放置在第二组上方
ax.set_theta_offset(pi / 2)
ax.set_theta_direction(-1)

# 设置图上各参数
plt.xticks(angles[:-1], cate, color='grey', size=15)
ax.set_rlabel_position(0)
plt.yticks([10,20,30], ["10","20","30"], color="grey", size=8)
plt.ylim(0,40)

# 绘制雷达图-A组
values=df.loc[0].drop('group').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="group A")
ax.fill(angles, values, 'b', alpha=0.1)

# 绘制雷达图-B组
values=df.loc[1].drop('group').values.flatten().tolist()
values += values[:1]
ax.plot(angles, values, linewidth=1, linestyle='solid', label="group B")
ax.fill(angles, values, 'r', alpha=0.1)

# 图例
plt.legend(loc='upper right', bbox_to_anchor=(0.1, 0.1))
plt.show()

OUTPUT:

 3 绘制环形图(甜甜圈图)

import matplotlib.pyplot as plt

# 创建数据
size_of_groups=[12,15,9,39]

# 先绘制饼图
plt.pie(size_of_groups)

# 在饼图中添加白色的圆
my_circle=plt.Circle( (0,0), 0.7, color='white')
p=plt.gcf()
p.gca().add_artist(my_circle)
plt.show()

OUTPUT:

 4 绘制时间序列图

4.1 时间序列图

import matplotlib.pyplot as plt
import numpy as np

# 创建数据

data=np.cumsum(np.random.randn(1000,1))

# 绘制

plt.plot(data)

OUTPUT:

4.2 多组时间序列图

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
 
# 创建数据
df=pd.DataFrame({'x_values': range(1,11), 'y1_values': np.random.randn(10), 'y2_values': np.random.randn(10)+range(1,11), 'y3_values': np.random.randn(10)+range(11,21) })
 
# 绘制多线时间序列图
plt.plot( 'x_values', 'y1_values', data=df, marker='', color='olive', linewidth=2, label="f1")
plt.plot( 'x_values', 'y2_values', data=df, marker='', color='olive', linewidth=2, label="f2")
plt.plot( 'x_values', 'y3_values', data=df, marker='', color='olive', linewidth=2, linestyle='dashed', label="f3")
plt.legend()
plt.show()

OUTPUT:   

 5 面积图

import numpy as np
import matplotlib.pyplot as plt

# 创建数据
x=range(1,30)
y=[1,3,6,9,4,9,5,6,8,3,5,3,2,6,1,5,6,8,7,4,2,6,2,5,8,7,3,9,8]

# 绘制并设置图形相关参数
plt.fill_between( x, y, color="skyblue", alpha=0.2)
plt.plot(x, y, color="Slateblue", alpha=0.6)
plot.show()

OUTPUT:   

标签:plt,Python,Matplotlib,df,Part2,values,angles,np,import
来源: https://blog.csdn.net/Luminoll/article/details/122735049