1063. Set Similarity (25)

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

#define N 51

int countcommon(vector<int> &_s1, vector<int> &_s2) {
	vector<int> *p1 = &_s1, *p2 = &_s2;
	if(p1->size() > p2->size())
		swap(p1, p2);
	vector<int> &s1 = *p1, &s2 = *p2;

	int count = 0;
	int d = -1;
	for(int i = 0; i < s1.size(); i ++) {
		if(s1[i] != d) {
			d = s1[i];
			if(binary_search(s2.begin(), s2.end(), d))
				count ++;
		}
	}
	return count;
}

int main(int argc, char **argv) {
	int n;
	cin >> n;

	vector<int> ns[N];
	int count[N] = {};
	for(int i = 1; i <= n; i ++) {
		int m;
		cin >> m;
		ns[i].reserve(m);
		for(int j = 0; j < m; j ++) {
			int d;
			scanf("%d", &d);
			ns[i].push_back(d);
		}
		sort(ns[i].begin(), ns[i].end());
		int d = -1;
		for(int j = 0; j < m; j ++) {
			if(ns[i][j] != d) {
				count[i] ++;
				d = ns[i][j];
			}
		}
	}

	int k;
	cin >> k;
	for(int i = 0; i < k; i ++) {
		int s1, s2;
		scanf("%d%d", &s1, &s2);
		int total = count[s1] + count[s2];
		int common = countcommon(ns[s1], ns[s2]);
		printf("%.1lf%%\n", 100.0*common/(total-common));
	}

	return 0;
}

要在这个代码中添加图例,需要使用plt.legend()函数来创建图例。首先,在ax1.bar()和ax2.bar()函数中添加label参数,将每个柱形的标签设置为相应的名称。然后,在创建图例之前,需要将ax1和ax2的对象存储在变量中,以便在plt.legend()函数中使用。最后,使用plt.legend()函数并设置loc参数来指定图例的位置。 以下是修改后的代码: ``` import matplotlib.pyplot as plt # 数据 similarity = [0.3940893515407449, 1.0116279478167544] divergence = [8.761, 24.455] labels = ['w. cu', 'wo. cu'] # 设置字体大小 plt.rcParams['axes.labelsize'] = 16 plt.rcParams['xtick.labelsize'] = 12 plt.rcParams['ytick.labelsize'] = 14 plt.rcParams['legend.fontsize'] = 12 # 柱形宽度和间隔 width = 0.3 x1_list = [] x2_list = [] for i in range(len(similarity)): x1_list.append(i) x2_list.append(i + width) # 创建图层 fig, ax1 = plt.subplots() # 设置左侧Y轴对应的figure ax1.set_ylabel('Time per iteration (s)') ax1.set_ylim(0, 1.1) ax1.bar(x1_list, similarity, width=width, color='tab:red', align='edge', label='Similarity') ax1.set_xticklabels(ax1.get_xticklabels()) # 设置右侧Y轴对应的figure ax2 = ax1.twinx() ax2.set_ylabel('GPU Usage (GB)') ax2.set_ylim(0, 28) ax2.bar(x2_list, divergence, width=width, color='tab:blue', align='edge', tick_label=labels, label='Divergence') # 添加图例 ax1_legend = ax1.legend(loc='upper left') ax2_legend = ax2.legend(loc='upper right') # 显示图表 plt.tight_layout() plt.savefig("similarity.png") plt.show() ``` 这将在图表中添加两个图例,分别显示“Similarity”和“Divergence”标签,并将它们与相应的柱形颜色匹配。注意,在添加图例之前,需要将ax1和ax2的对象存储在变量中,并在plt.legend()函数中分别使用它们来创建图例。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值