Python:3D 散射丢失颜色图 [英] Python: 3D scatter losing colormap

查看:34
本文介绍了Python:3D 散射丢失颜色图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含多组数据的 3D 散点图,并为整个图形使用颜色图.代码如下所示:

将 matplotlib.pyplot 导入为 plt从 mpl_toolkits.mplot3d 导入 Axes3Dfig = plt.figure()ax = fig.add_subplot(111, 投影='3d')对于 [range(0,10), range(5,15), range(10,20)] 中的 R:数据 = [np.array(R), np.array(range(10)), np.array(range(10))]AX = ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet)def forceUpdate(event): AX.changed()fig.canvas.mpl_connect('draw_event', forceUpdate)plt.colorbar(AX)

这很好用,但是一旦我保存它或旋转绘图,第一个和第二个散点上的颜色就会变成蓝色.

强制更新通过保留颜色但仅在绘制的最后一个散点图上起作用.我尝试制作一个更新所有散点图的循环,但得到与上述相同的结果:

AX = []对于 [range(0,10), range(5,15), range(10,20)] 中的 R:数据 = [np.array(R), np.array(range(10)), np.array(range(10))]AX.append(ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet))对于 AX 中的 i:def forceUpdate(event): i.changed()fig.canvas.mpl_connect('draw_event', forceUpdate)

知道如何确保所有散点图都被更新从而颜色不会消失吗?

谢谢!

解决方案

已修改您的代码,使其可以执行任何操作:

<预><代码>>>>将 numpy 导入为 np>>>导入 matplotlib.pyplot 作为 plt>>>从 mpl_toolkits.mplot3d 导入 Axes3D>>>AX = []>>>fig = plt.figure()>>>ax = fig.add_subplot(111, 投影='3d')>>>对于 [range(0,10), range(5,15), range(10,20)] 中的 R:... 数据 = [np.array(R), np.array(range(10)), np.array(range(10))]... AX = ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet)... def forceUpdate(event): AX.changed()... fig.canvas.mpl_connect('draw_event', forceUpdate)...91011>>>plt.colorbar(AX)<matplotlib.colorbar.Colorbar 实例在 0x36265a8>>>>plt.show()

然后我得到:

所以上面的代码是有效的.如果您现有的代码不是,那么我建议您尝试上面的确切代码,如果这不起作用,请查看您正在使用的代码版本,如果它起作用,那么您将不得不调查它与您的代码之间的差异实际代码,(而不是您的示例代码).

I'm creating a 3D scatter plot with multiple sets of data and using a colormap for the whole figure. The code looks like this:

import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

for R in [range(0,10), range(5,15), range(10,20)]:
  data = [np.array(R), np.array(range(10)), np.array(range(10))]
  AX = ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet)
  def forceUpdate(event): AX.changed()
  fig.canvas.mpl_connect('draw_event', forceUpdate)

plt.colorbar(AX)

This works fine but as soon as I save it or rotate the plot, the colors on the first and second scatters turn blue.

The force update is working by keeping the colors but only on the last scatter plot drawn. I tried making a loop that updates all the scatter plots but I get the same result as above:

AX = []
for R in [range(0,10), range(5,15), range(10,20)]:
  data = [np.array(R), np.array(range(10)), np.array(range(10))]
  AX.append(ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet))
for i in AX:
  def forceUpdate(event): i.changed()
  fig.canvas.mpl_connect('draw_event', forceUpdate)

Any idea how I can make sure all scatters are being updated so the colors don't disappear?

Thanks!

解决方案

Having modified your code so that it does anything:

>>> import numpy as np
>>> import matplotlib.pyplot as plt
>>> from mpl_toolkits.mplot3d import Axes3D
>>> AX = []
>>> fig = plt.figure()
>>> ax = fig.add_subplot(111, projection='3d')
>>> for R in [range(0,10), range(5,15), range(10,20)]:
...   data = [np.array(R), np.array(range(10)), np.array(range(10))]
...   AX = ax.scatter(*data, c=data[0], vmin=0, vmax=20, cmap=plt.cm.jet)
...   def forceUpdate(event): AX.changed()
...   fig.canvas.mpl_connect('draw_event', forceUpdate)
... 
9
10
11
>>> plt.colorbar(AX)
<matplotlib.colorbar.Colorbar instance at 0x36265a8>
>>> plt.show()

then I get:

So the above code is working. If your existing code isn't then I suggest that you try the exact code above and if that doesn't work look into the versions of code that you are using if it does work then you will have to investigate the differences between it and your actual code, (rather than your example code).

这篇关于Python:3D 散射丢失颜色图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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