简体   繁体   English

在3D散点图上绘制多个数据

[英]Plot multiple data on 3d scatter plot

I'm having trouble plotting multiple sets of data onto a single 3D scatter plot. 我在将多组数据绘制到单个3D散点图上遇到麻烦。 What I'm doing is I have a system of three equations and I'm calculating the zeros of the equations using linalg. 我正在做的是,我有一个由三个方程组成的系统,并且我正在使用linalg计算方程的零点。 I'm then plotting each set of zeros I get onto a 3D plot. 然后,我将绘制的每个零集绘制到3D图上。 For one of my parameters, I'm changing it's value and observing how the zeros change from that. 对于我的参数之一,我正在更改它的值,并观察零从此如何变化。 I'd like to plot all of the data sets on one 3D scatter plot so it'd be easy to compare how they differ but I keep getting one graph plotted for each data set. 我想在一个3D散点图上绘制所有数据集,以便比较它们之间的差异很容易,但是我不断为每个数据集绘制一张图。 Can any of you figure out what I need to fix? 你们中有人可以找出我需要解决的问题吗?

import numpy as np
from numpy import linalg
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

plt.close('all')
#Will be solving the following system of equations:
#sx-(b/r)z=0
#-x+ry+(s-b)z=0
#(1/r)x+y-z=0

r=50.0
b=17.0/4.0
s=[10.0,20.0,7.0,r/b]

color=['r','b','g','y']
markers=['s','o','^','d']

def system(s,b,r,color,m):
#first creates the matrix as an array so the parameters can be changed from outside
#and then coverts array into a matrix
    u_arr=np.array([[s,0,-b/r],[-1,r,s-b],[1/r,1,-1]])
    u_mat=np.matrix(u_arr)

    U_mat=linalg.inv(u_mat)

    #converts matrix into an array and then into a list to manipulate
    x_zeros=np.array(U_mat[0]).reshape(-1).tolist()
    y_zeros=np.array(U_mat[1]).reshape(-1).tolist()
    z_zeros=np.array(U_mat[2]).reshape(-1).tolist()

    zeros=[x_zeros,y_zeros,z_zeros]
    coordinates=['x','y','z']

    print('+'*70)
    print('For s=%1.1f:' % s)
    print('\n')

    for i in range(3):
        print('For the %s direction, the roots are: ' % coordinates[i])
        for j in range(3):
            print(zeros[i][j])
        print('-'*50)

    fig3d=plt.figure()
    ax=Axes3D(fig3d)
    ax.scatter(x_zeros,y_zeros,z_zeros,c=color,marker=m)
    plt.title('Zeros for a Given System of Equations for s=%1.1f' % (s))
    ax.set_xlabel('Zeros in x Direction')
    ax.set_ylabel('Zeros in y Direction')
    ax.set_zlabel('Zeros in z Direction')
    plt.show()

for k in range(len(s)):
    system(s[k],b,r,color[k],markers[k])

Thanks in advance for any help. 在此先感谢您的帮助。

You are creating a new axes instance each time system() is called. 每次调用system()时,您都在创建一个新的轴实例。 Instead pass ax as an argument to system 而是将ax作为参数传递给system

def system(s,b,r,color,m, ax):

        # ...
        ax.scatter(x_zeros,y_zeros,z_zeros,c=color,marker=m)

Then create the axes instance before looping 然后在循环之前创建轴实例

fig3d=plt.figure()
ax=Axes3D(fig3d)

for k in range(len(s)):
    system(s[k],b,r,color[k],markers[k], ax)

plt.show()

This was all plots are added to ax . 这是将所有地块都添加到ax You may then want to think about setting the axes labels etc outside of the system() function. 然后,您可能需要考虑在system()函数外部设置轴标签等。 Splitting it into two functions, one which sets the plot up and one which creates the required data and plots it. 将其分为两个功能,一个功能设置图表,另一个功能创建所需的数据并将其绘制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM