Python | Matplotlib | 可视化合集Part1

本文介绍了使用Python的Matplotlib库进行数据可视化的多个方面,包括绘制折线图、点状图、条形图、散点图、饼图、直方图、箱型图、热图和气泡图。通过实例展示了各种图表的创建方法,为数据可视化提供了丰富的示例。
摘要由CSDN通过智能技术生成

1 绘制折线图

1.1 折线图

import matplotlib.pyplot as plt
 
# 绘制折线图
plt.plot([10, 20])

# 折线图与轴命名
plt.title("Line Graph")
plt.xlabel("X axis")
plt.ylabel("Y axis")
plt.show()

OUTPUT:

 1.2 多线折线图

import matplotlib.pyplot as plt

# 绘制折线图
plt.plot([10, 20, 16, 18], label='Tea')
plt.plot([2, 7, 9, 8], label='Alcohol')
plt.plot([8.8, 12.7, 11.5, 9.9], label='Milk')
plt.plot([1.3, 4.9, 6.1, 3.9, 7.9], label='Coffee')

# 多线折线图与轴命名
plt.title("Multi Line Graph")
plt.xlabel("X axis")
plt.ylabel("Y axis")

plt.legend()
plt.show()

OUTPUT:

2 绘制点状图

import matplotlib.pyplot as plt
 
x = [1, 2, 5, 7, 9, 11, 15, 18]
y = [2, 3, 5, 6, 10, 12, 13, 17]
 
# 绘制虚线图
plt.plot(x, y, label='Tea', c='teal', ls=('dashed'), lw=2.5)
 
# 绘制点线图
plt.plot(y, x, label='Coffee', c='red', ls=('dotted'), lw=2.5)
 
plt.legend()
plt.show()

OUTPUT:

### 回答1: 定向倒角匹配算法是用来在两个有向图之间寻找最大匹配的算法。 以下是用 Python 实现定向倒角匹配算法的示例代码: ``` from collections import defaultdict def find_matching(graph, matching, u, visited): """ Finds a matching for a given vertex using DFS. """ for v in graph[u]: if visited[v]: continue visited[v] = True if matching[v] == -1 or find_matching(graph, matching, matching[v], visited): matching[v] = u return True return False def max_matching(graph, n, m): """ Finds the maximum matching in a bipartite graph. """ # Initialize the matching to be empty. matching = [-1] * m # Keep track of the number of matchings found. matchings_count = 0 # Iterate through all vertices in the left part of the bipartite graph. for u in range(n): # Reset the visited array for each iteration. visited = [False] * m # Try to find a matching for the current vertex. if find_matching(graph, matching, u, visited): matchings_count += 1 return matchings_count, matching def visualize_matching(graph, matching, n, m): """ Visualizes the matching in a bipartite graph. """ # Initialize a dictionary to store the matchings for each vertex. matchings = defaultdict(list) # Iterate through all vertices in the right part of the bipartite graph. for v in range(m): # If the vertex is part of the matching, add it to the dictionary. if matching[v] != -1: matchings[matching[v]].append(v) # Print the matchings for each vertex. for u in range(n): print(f"Vertex {u} is matched with vertices {matchings[u]}") if __name__ == "__main__": # Example bipartite graph. graph = defaultdict(list) graph[0] = [1, 2] graph[1] = [2] graph[2] = [0, 3] graph[3] = [3] # Number of vertices in the left and right parts of the bipartite graph. n = 4 m = 4 # Find the maximum matching in the bipartite graph. matchings_count, matching = max_matching(graph, n, m) print(f"Found {matchings_count} matchings.") # Visualize the matching. visualize_matching(graph, matching, n, m) ``` 在上面的代码中 ### 回答2: 定向倒角匹配算法是一种用于图像处理中的特征匹配算法。在Python中,我们可以使用OpenCV库来实现该算法,并通过Matplotlib库来可视化每个点的匹配关系。 首先,我们需要导入所需的库: ```python import cv2 import numpy as np import matplotlib.pyplot as plt ``` 然后,我们可以定义一个函数来实现定向倒角匹配算法: ```python def directed_corner_matching(image1, image2): # 将图像转换为灰度图 gray1 = cv2.cvtColor(image1, cv2.COLOR_BGR2GRAY) gray2 = cv2.cvtColor(image2, cv2.COLOR_BGR2GRAY) # 创建SIFT特征提取器 sift = cv2.SIFT_create() # 检测关键点和计算其描述符 keypoints1, descriptors1 = sift.detectAndCompute(gray1, None) keypoints2, descriptors2 = sift.detectAndCompute(gray2, None) # 创建BFMatcher对象 bf = cv2.BFMatcher() # 使用knnMatch方法寻找最佳匹配 matches = bf.knnMatch(descriptors1, descriptors2, k=2) # 应用比例测试以获取最好的匹配 good_matches = [] for m,n in matches: if m.distance < 0.75*n.distance: good_matches.append(m) # 可视化匹配结果 result = cv2.drawMatches(image1, keypoints1, image2, keypoints2, good_matches, None, flags=2) plt.imshow(result) plt.axis('off') plt.show() ``` 最后,我们可以加载两个图像并调用该函数来执行匹配算法并可视化结果: ```python image1 = cv2.imread('image1.jpg') image2 = cv2.imread('image2.jpg') directed_corner_matching(image1, image2) ``` 这样,我们就实现了用Python实现定向倒角匹配算法,并可视化每个点的匹配关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值