matplotlib 在 3D 中绘制多条线 [英] matplotlib plotting multiple lines in 3D

查看:175
本文介绍了matplotlib 在 3D 中绘制多条线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 matplotlib 在 3D 图中绘制多条线.我有x和y值的6个数据集.到目前为止我所尝试的是,给数据集中的每个点一个 z 值.因此,数据集 1 中的所有点都具有 z=1,数据集 2 中的所有点都具有 z=2,依此类推.然后,我将它们导出到三个文件中.X.txt"包含所有 x 值,Y.txt"包含所有 y 值,Z.txt"相同.

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".

这是目前的代码:

        #!/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?

推荐答案

单独加载数据集,然后单独绘制每个数据集.

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()

每次您在 axes 对象上调用 plot (或 plot_wireframe 但我不知道您需要什么)时,它将将数据添加为新系列.如果你省略 color 参数 matplotlib 会为你选择它们,但它不是太聪明,在你添加太多系列后它会循环并开始使用相同的再次上色.

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.我没有测试过这个 - 不记得 color 是否是正确的参数.可以肯定的是.

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

这篇关于matplotlib 在 3D 中绘制多条线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆