Python | Matplotlib | 可视化合集Part2
2022/1/29 11:05:25
本文主要是介绍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:
这篇关于Python | Matplotlib | 可视化合集Part2的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享