程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了在 x 和 y 值上使用不同大小的数组馈送 pyplot大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决在 x 和 y 值上使用不同大小的数组馈送 pyplot?

开发过程中遇到在 x 和 y 值上使用不同大小的数组馈送 pyplot的问题如何解决?下面主要结合日常开发的经验,给出你关于在 x 和 y 值上使用不同大小的数组馈送 pyplot的解决方法建议,希望对你解决在 x 和 y 值上使用不同大小的数组馈送 pyplot有所启发或帮助;

我有两个 numpy 数组。一个用于 x 轴条目,另一个用于 y 轴,如下面的代码所示

    plt.figure(figsize=(10,10))
    plt.plot(range(0,len(TVals_R)),TVals,'bo',markersize=1,label='Dry Run') #I need x and y arrays in different size here
    plt.figure(figsize=(10,10))
    plt.ylabel('Temperature ($^\circ$C)')
    plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")
    plt.legend(loc="upper right")

在 x 轴上,我想使用比 y 数组更大的数字,例如 len(TVals_R)。因为我将在图表中添加两条不同的 x 轴范围。但它返回错误 ValueError: x and y must have same first dimension,but have shapes (920,) and (498,)

有没有办法在 pylot 上使用不同大小的列表?

我还尝试使用不同的轴在图中添加两条不同大小的线,但由于我有第三条线,我无法使用它。这是我尝试过的

    plt.figure(figsize=(10,10))
    fig,ax1=plt.subplots()
    ax2=ax1.twiny()
    ax3=ax1.twiny()
    curve1,= ax1.plot(range(0,len(TVals)),label='Dry Run')
    curve2,= ax2.plot(range(0,TVals_R,'ro',label='Radiation Run')
    curve3,= ax3.plot(range(0,TVals_interpolated_R,'go',label='handHeld meter and \n linear interpolation')
    curves = [curve1,curve2,curve3]
    ax2.legend(curves,[curve.get_label() for curve in curves]) 
    ax1.set_xlabel('Measurement',color=curve1.get_color()) 
    ax2.set_xlabel('Measurement',color=curve2.get_color())
    ax1.set_ylabel('Temperature ($^\circ$C)')  
    plt.ylabel('Temperature ($^\circ$C)')
    #plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")

返回错误 ValueError: x and y must have same first dimension,)

我没有将轴分成 ax1、ax2 等,而是尝试将空元素添加到较小的列表中以匹配最大的列表,但是在 numpy 中附加 [] 添加了 0 (zero),这在我的数据中具有误导性

感谢您对这两种方法的任何帮助。

解决方法

看起来最好的方法是忽略错误,因此可以重叠具有不同 x 轴范围的 2 个图。而且这个解决方案不需要无花果、斧头分离。

    plt.figure(figsize=(10,10))
    try:
        plt.plot(range(0,len(TVals)),TVals,'o',markersize=1,label='Dry Run')
        plt.plot(range(0,len(TVals_R)),TVals_R,'ro',label='Radiation Run')
        plt.plot(range(0,TVals_interpolated,'go',label='handHeld meter and \n linear interpolation')
    except ValueError:
            pass
    plt.ylabel('Temperature ($^\circ$C)')
    plt.xlabel('Measurement')
    plt.title("Temperature vs. Measurement")
    plt.legend(loc="upper right")
    plt.show()

大佬总结

以上是大佬教程为你收集整理的在 x 和 y 值上使用不同大小的数组馈送 pyplot全部内容,希望文章能够帮你解决在 x 和 y 值上使用不同大小的数组馈送 pyplot所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。