【问题标题】:Adjusting Plotted Values of Contour Plots调整等高线图的绘制值
【发布时间】:2022-01-05 11:30:23
【问题描述】:

我正在制作等高线图,这些图基本上是流体动力学系统的解析或数值解。我不认为技术的东西真的太重要了,但这是我的情节。第一个图是数值(矩阵系统)解决方案,第二个图是漂亮的封闭形式(单论坛)解决方案。

可以看出,我的第二个情节在右侧有气泡。查看图例/比例,我有负值。我不想有负值,或者不绘制它们,尽管我不确定如何在我的代码中调整它。我花了一些时间研究如何将 z 值调整为仅正数,但我似乎无法理解。我将删除我的绘图代码,然后删除我在绘图中使用的漂亮的封闭形式函数。

import numpy as np 
import matplotlib.pyplot as plt
import scipy as sp
import scipy.special as sp1
from mpl_toolkits.mplot3d import Axes3D

def v(r,z,gamma):
    a=r*(1-z/gamma) 
    sums = 0
    for n in range(1,26):
        sums += ((sp1.iv(1(n*np.pi*r)/gamma))/(n*sp1.iv(1(n*np.pi)/gamma)))*np.sin(n*np.pi*z/gamma)    
    return a-(2/np.pi)*sums

def plot_contour(a, filename=None, zlabel='v(r,z)',cmap=plt.cm.gnuplot):
    fig = plt.figure(figsize=(5,4))
    ax = fig.add_subplot(111)

    x = np.arange(a.shape[0])
    y = np.arange(a.shape[1])
    X, Y = np.meshgrid(x, y)
    Z = a[X, Y]
    cset = ax.contourf(X, Y, Z, 20, cmap=cmap)
    ax.set_xlabel('r')
    ax.set_ylabel('z')
    ax.set_title('\u0393=2.5')
    ax.axis('off')
    ax.set_aspect(1)

    cb = fig.colorbar(cset, shrink=0.5, aspect=5)
    cb.set_label(zlabel)
    
    if filename:
        fig.savefig(filename,dpi=1600)
        plt.close(fig)
        return filename
    else:
        return ax
...
plot_contour(v1, 'gamma25e+1')

这是所有必要的代码。剩下的就是矩阵解的东西,只是一堆线性代数。关于我需要添加或调整的任何帮助,以防止负值出现在第二个图上。它应该看起来和第一个完全一样。

【问题讨论】:

    标签: python matplotlib plot contour


    【解决方案1】:

    我花了一些时间研究如何将 z 值调整为仅正数

    你能做什么很大程度上取决于你想对低于零的结果做什么,如果你的唯一目的是让低于零的点显示为零,你可以简单地将它们设为零,但这会显示错误结果。

        x = np.arange(a.shape[0])
        y = np.arange(a.shape[1])
        X, Y = np.meshgrid(x, y)
        Z = a[X, Y]
        Z[Z < 0] = 0
    

    另一种解决方案是减去数据的最小值,使结果的最小值为 0。

        x = np.arange(a.shape[0])
        y = np.arange(a.shape[1])
        X, Y = np.meshgrid(x, y)
        Z = a[X, Y]
        Z -= np.amin(Z)
    

    【讨论】:

    • 我确实测试了你建议的两种方法,你是对的,显示为零,并没有完全删除绘图上的形状,它只是显示在同一级别。减去最小值似乎会在图中给出新的负区域。所以,我猜问题的本质在于被评估的功能。我的函数 v(r,z,gamma) 给出负值。我将不得不尝试修复这个错误,但你回答了我问的情节问题,所以谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-15
    • 2018-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多