Python,改变3D绘图的轴

时间:2017-04-13 02:30:48

标签: python matplotlib plot graph

我有以下代码:

ax.scatter(t,d,p)

t和p是列表(时间和价格),d是整数(天)。当我运行代码时,我得到的结果如下:

enter image description here

问题是轴是错误的。 p和d需要交换但是 当我尝试做的时候:

 constructor(props) {
    super(props);

    this.state = {
        hidden: true,
        value: props.value
    };
}
...
 render() {

 return <div className="form-group">
     <Input  onClick={() => this.showEditor()}  onChange={() => false} value={this.props.value}/>
     <div className={classNames({'hidden': this.state.hidden})}>
     <ReactAceEditor
        onLoad={(editor) => {
          editor.focus();
        }} 
     />
   </div>
   </div>
}

我收到错误提示“参数xs和ys必须大小相同”。有没有什么方法可以让轴被切换,因为直观地说这个图在这个配置中没有意义。

迭代日期的原因是我可以在图上每天使用单独的颜色。

我尝试了每天迭代t和p列表的解决方案,并且只绘制了相应的t,d,p点,但是这样慢得多,之后如果你尝试移动它,matplotlib图就会没有响应。

1 个答案:

答案 0 :(得分:1)

我不确定您收到错误消息的原因,但是您能提供一份数据样本吗?以下代码工作正常,并生成您要求的图表类型。

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

# Generate some dummy data
time = np.random.rand(100)
price = 120+10*np.random.rand(100)
day = np.random.randint(0,10,100)


# Plot data
fig = plt.figure(figsize=(12,4))

ax = fig.add_subplot(121, projection='3d')
ax.scatter(time, price, day)
ax.set_xlabel('time')
ax.set_ylabel('price')
ax.set_zlabel('day')

ax = fig.add_subplot(122, projection='3d')
ax.scatter(time, day, price)
ax.set_xlabel('time')
ax.set_ylabel('day')
ax.set_zlabel('price')

fig.show()

enter image description here

编辑:

您可以通过传递列表/数组来设置散点图中点的颜色。如果我们使用以下方法绘制第二个散点图:

ax.scatter(time, day, price, c=day)

我们得到: enter image description here