Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

matplotlib plotting multiple lines in 3D

I am trying to plot multiple lines in a 3D plot using matplotlib. I have 6 datasets with x and y values. What I've tried so far was, to give each point in the data sets a z-value. So all points in data set 1 have z=1 all points of data set 2 have z=2 and so on. Then I exported them into three files. "X.txt" containing all x-values, "Y.txt" containing all y-values, same for "Z.txt".

Here's the code so far:

        #!/usr/bin/python
        from mpl_toolkits.mplot3d import axes3d
        import matplotlib.pyplot as plt
        import numpy as np
        import pylab

        xdata = '/X.txt'
        ydata = '/Y.txt'
        zdata = '/Z.txt'

        X = np.loadtxt(xdata)
        Y = np.loadtxt(ydata)
        Z = np.loadtxt(zdata)

        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.plot_wireframe(X,Y,Z)
        plt.show()

What I get looks pretty close to what I need. But when using wireframe, the first point and the last point of each dataset are connected. How can I change the colour of the line for each data set and how can I remove the connecting lines between the datasets?

Is there a better plotting style then wireframe?

like image 621
WMrioa Avatar asked Oct 30 '13 09:10

WMrioa


People also ask

How do you plot multiple lines in python 3D?

You can plot multiple lines in 3D in python using matplotlib and by importing the mplot3d submodule from the module mpl_toolkits, an external toolkit for matplotlib in python used to plot the multi-vectors of geometric algebra.

Can matplotlib Pyplot be used to display 3D plots?

Matplotlib was introduced keeping in mind, only two-dimensional plotting. But at the time when the release of 1.0 occurred, the 3d utilities were developed upon the 2d and thus, we have 3d implementation of data available today! The 3d plots are enabled by importing the mplot3d toolkit.

How to plot a 3D plot in Python using matplotlib?

Note that these two steps will be common in most of the 3D plotting you do in Python using Matplotlib. After we create the axes object, we can use it to create any type of plot we want in the 3D space. To plot a single point, we will use the scatter () method, and pass the three coordinates of the point.

How to draw multiple lines in Matplotlib?

To draw multiple lines we will use different functions which are as follows: y = x x = y y = sin(x) y = cos(x) Python3 # importing package importmatplotlib.pyplot as plt importnumpy as np # create data x =[1,2,3,4,5] y =[3,3,3,3,3] # plot lines plt.plot(x, y, label ="line 1") plt.plot(y, x, label ="line 2")

How to create contour plot in Matplotlib?

Contour plot is a way of showing a 3D graph by plotting constant z-slices. Filled contour fills the areas that were shown by the line in contour plots. The first step is to import all the necessary packages for plotting the above plot. Apart from matplotlib.pyplot and NumPy, we are importing another package which is mpl_toolkits.mplot3d.

How do you make a line plot in Python?

Line plot: Line plots can be created in Python with Matplotlib’s pyplot library. To build a line plot, first import Matplotlib. It is a standard convention to import Matplotlib’s pyplot library as plt. The plt alias will be familiar to other Python programmers.


1 Answers

Load the data sets individually, and then plot each one individually.

I don't know what formats you have, but you want something like this

from mpl_toolkits.mplot3d.axes3d import Axes3D
import matplotlib.pyplot as plt
fig, ax = plt.subplots(subplot_kw={'projection': '3d'})

datasets = [{"x":[1,2,3], "y":[1,4,9], "z":[0,0,0], "colour": "red"} for _ in range(6)]

for dataset in datasets:
    ax.plot(dataset["x"], dataset["y"], dataset["z"], color=dataset["colour"])

plt.show()

Each time you call plot (or plot_wireframe but i don't know what you need that) on an axes object, it will add the data as a new series. If you leave out the color argument matplotlib will choose them for you, but it's not too smart and after you add too many series' it will loop around and start using the same colours again.

n.b. i haven't tested this - can't remember if color is the correct argument. Pretty sure it is though.

like image 78
will Avatar answered Oct 07 '22 02:10

will