Python的:三维散失去颜色表颜色、Python

2023-09-08 01:12:24 作者:相关热门的>>

我创建一个3D散点图多组数据,并使用颜色表为全数字。在code是这样的:

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?

谢谢!

推荐答案

已经修改了code,以便它做任何事情:

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:

所以上面的code的工作。如果您现有的code不是那么我建议你试试上面的,如果不工作考虑您正在使用code版本,如果它不工作,那么你将确切的code要调查它和你的实际code之间的差异,(而不是你的榜样code)。

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

 
精彩推荐
图片推荐