Python绘制多条y轴范围不同的曲线并在一张图上显示

如何使用Python绘制多条y轴范围不同的曲线,然后把它们合并在一张图上显示

import matplotlib.pyplot as plt
import numpy as np

def multilines(target, x, ys, types, colors, x_label, labels):
    """
    用来绘制多条y轴范围不同的线,并在一张图上显示
    """
    # fig, ax = plt.subplots(figsize=(16, 4), dpi=200)
    # fig.subplots_adjust(left=0.2)
    twins = []
    twins.append(target[0])
    p = []
    for i in range(len(ys) - 1):
        twins.append(target[0].twinx())
    for i, label in enumerate(labels):
        temp_p, = twins[i].plot(x, ys[i], types[i], color=colors[i], label=label)
        p.append(temp_p)

    twins[0].set_xlabel(x_label)
    # twins[0].tick_params(axis='x', labelsize=20)
    pad = 0
    twin_lines = []
    twin_labels = []
    for i, twin in enumerate(twins):
        twin.set_ylabel(labels[i])
        twin.yaxis.set_label_position('left')
        twin.yaxis.set_ticks_position('left')
        twin.yaxis.label.set_color(p[i].get_color())
        twin.tick_params(axis='y', colors=p[i].get_color(), pad=pad)
        pad += 60
        temp_line, temp_label = twin.get_legend_handles_labels()
        twin_lines.append(temp_line)
        twin_labels.append(temp_label)
    final_line = twin_lines[0]
    final_label = twin_labels[0]
    for i in range(len(twin_lines) - 1):
        final_line += twin_lines[i + 1]
        final_label += twin_labels[i + 1]
    twins[0].legend(final_line, final_label, loc='best')


if __name__ == '__main__':
    x = np.linspace(0, 10, 100)
    y1 = np.sin(x)
    y2 = 2 * np.cos(x)
    y3 = 0.5 * np.tan(x)
    fig, ax = plt.subplots(figsize=(16, 4), dpi=200)
    fig.subplots_adjust(left=0.2)
    multilines([ax], x, [y1, y2, y3], ['r-', 'b-', 'g-'], ['red', 'blue', 'green'], 'depth', ['y1', 'y2', 'y3'])
    plt.show()

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python绘制多条ROC曲线的方法如下: 1. 导入所需的库:`import matplotlib.pyplot as plt` 2. 准备数据:获取多个分类器的真正率(True Positive Rate, TPR)和假正率(False Positive Rate, FPR)曲线数据,这些数据可以通过调用分类器的评估函数获得。 3. 创建图像和子图对象:`fig, ax = plt.subplots()` 4. 循环绘制曲线:使用`ax.plot()`函数,一次绘制每个分类器的ROC曲线,传入分类器的TPR和FPR数据作为参数。 5. 添加标题和标签:使用`ax.set()`函数,设置图像的标题、X轴和Y轴的标签。 6. 设置图例:如果需要,使用`ax.legend()`函数,设置图例以显示每个分类器的标识。 7. 显示图像:使用`plt.show()`函数,显示绘制好的ROC曲线图像。 下面是一个简单的示例代码,其中包含了绘制两条ROC曲线的基本步骤: ```python import matplotlib.pyplot as plt # 准备数据 classifier_1_tpr = [0.1, 0.2, 0.4, 0.6, 0.8] # 分类器1的TPR数据 classifier_1_fpr = [0.2, 0.3, 0.5, 0.7, 0.9] # 分类器1的FPR数据 classifier_2_tpr = [0.2, 0.3, 0.5, 0.7, 0.9] # 分类器2的TPR数据 classifier_2_fpr = [0.1, 0.2, 0.4, 0.6, 0.8] # 分类器2的FPR数据 # 创建图像和子图对象 fig, ax = plt.subplots() # 绘制曲线 ax.plot(classifier_1_fpr, classifier_1_tpr, label='Classifier 1') ax.plot(classifier_2_fpr, classifier_2_tpr, label='Classifier 2') # 添加标题和标签 ax.set(title='ROC Curve', xlabel='False Positive Rate', ylabel='True Positive Rate') # 设置图例 ax.legend() # 显示图像 plt.show() ``` 以上代码中,我们首先准备了两个分类器的TPR和FPR数据,然后使用`ax.plot()`函数绘制两条ROC曲线,接着用`ax.set()`函数设置了标题和标签,最后使用`ax.legend()`函数设置了图例,最终使用`plt.show()`函数显示绘制好的ROC曲线图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值