[Python] Plot multiple sets of data with tripcolor - transparency in colormaps

Discussão em 'Python' iniciado por Stack, Março 22, 2025.

  1. Stack

    Stack Membro Participativo

    I seem unable to solve this problem. I have multiple sets of data with the same x,y points. I do Delaunay triangulation and plot it with tripcolor. I read on this forum, that to plot several sets of data into the same plot, I need to specify the axis and stuff, but it doesn't seem to work for me here (beginner at matplotlib, sorry). My code:

    import matplotlib.pyplot as plt
    import matplotlib.tri as tri
    from pylab import genfromtxt

    data=genfromtxt("momentAF.txt")
    data2=genfromtxt("momentZZ.txt")

    x = data[:,0]
    y = data[:,1]
    z1 = data[:,4]
    z2 = data2[:,4]

    circle=plt.Circle((0,0),1,color="black",fill=False)

    triang = tri.Triangulation(x, y)

    ax = plt.subplot(111)
    ax.set_aspect('equal')
    ax.add_artist(circle)
    ax.tripcolor(triang, z2**2, shading='gouraud', cmap='Greens')
    ax.tripcolor(triang, z1**2, shading='gouraud', cmap='Reds')
    #plt.colorbar()
    plt.title("ST, G'=-0.0")


    Any help is greatly appreciated, thanks.

    EDIT: A rough example of what I'm going for: [​IMG]

    EDIT2: I tried to display the several data sets without setting transparency for all of them using colormap option "set_under":

    my_cmap = cm.get_cmap("Greens")
    my_cmap2 = cm.get_cmap("Reds")
    my_cmap.set_under('w',alpha=0)
    my_cmap2.set_under('w',alpha=0)


    and then

    ax.tripcolor(triang, z1**2, shading='gouraud', cmap=my_cmap, vmin=0.01)
    ax.tripcolor(triang, z2**2, shading='gouraud', cmap=my_cmap2, vmin=0.01)


    but it still only shows the region where z1 is non-zero (i.e. the region where z1 is non-zero is overwritten by white from my_cmap2, which I wanted to be transparent). I also tried masked arrays,

    masked = np.ma.masked_where(z1<.1,z3**2)
    masked2 = np.ma.masked_where(z2<.1,z4**2)

    ax.tripcolor(triang, masked, shading='gouraud', cmap="Greens")
    ax.tripcolor(triang, masked2, shading='gouraud', cmap="Reds")


    still to no avail though.

    Continue reading...

Compartilhe esta Página