Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7160389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T13:23:28+00:00 2026-05-28T13:23:28+00:00

Related to this question , I want a 3D scatter plot with prescribed colors

  • 0

Related to this question, I want a 3D scatter plot with prescribed colors for each point. The example posted in the question works on my system, but after the first redraw (for instance after saving or if I rotate the image) the color seems to be lost, i.e. all the points are drawn in blue color with the usual depth information. Please see the modified example below.

My system is Python 2.6.7 with matplotlib 1.1.0 installed from macports on a mac 10.8.0. I use the MacOSX backend.

Does anyone know how to circumvent this problem?

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

# Create Map
cm = plt.get_cmap("RdYlGn")

x = np.random.rand(30)
y = np.random.rand(30)
z = np.random.rand(30)

col = np.arange(30)

fig = plt.figure()
ax3D = fig.add_subplot(111, projection='3d')
ax3D.scatter(x, y, z, s=30, c=col, marker='o', cmap=cm)

plt.savefig('image1.png')
plt.savefig('image2.png')

Here are the two images, I get:
First image
Second image

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T13:23:28+00:00Added an answer on May 28, 2026 at 1:23 pm

    It’s not clear why this is happening, and it certainly is a bug. Here I provide a hack to get the result you want, though it is not as automatic as one would want.

    For some reason, the Patch3DCollection representing the scatter points is not updated after the first rendering. This update is essential, because it is where unique colors are set for each collection patch. To force it to reinitialize, you can use the changed method on the Patch3DCollection (really a ScalarMappable method), and this just documents that a change happend. When the figure is drawn, it checks if an update happened, and then it redefines the colors. If it didn’t, this process is skipped.

    To force this update to occur automatically, one would like to do this on every ‘draw’ event. To do this, one must register a method using the canvas‘s mpl_connect method (see linked tutorial).

    This example shows how saving the figure twice preserves the color mapping, but if you uncomment the plt.show() line, it will still work (on rotation for example).

    import numpy as np
    import matplotlib.pyplot as plt
    from mpl_toolkits.mplot3d import Axes3D
    
    # Create Map
    cm = plt.get_cmap("RdYlGn")
    
    # added a seed so consistant plotting of points
    np.random.seed(101)
    x = np.random.rand(30)
    y = np.random.rand(30)
    z = np.random.rand(30)
    
    col = np.arange(30)
    
    fig = plt.figure()
    #ax = fig.add_subplot(111)
    #scatCollection = ax.scatter(x,y,
    ax3D = fig.add_subplot(111, projection='3d')
    # keep track of the Patch3DCollection:
    scatCollection = ax3D.scatter(x, y, z, s=30, 
                                c=col, 
                                marker='o',
                                cmap=cm
                                )
    def forceUpdate(event):
        global scatCollection
        scatCollection.changed()
    
    fig.canvas.mpl_connect('draw_event',forceUpdate)
    
    #plt.show()
    
    plt.savefig('image1.png')
    
    plt.savefig('image2.png')
    

    Ideally it should not be required to do this, and the global scatCollection should be accessed using other methods (I’m working on doing this). But this works for now…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know this question isn't directly programming related, but since I want to be
Related to this question: URL characters replacement in JSP with UrlRewrite I want to
I want to expand on the functionality related to this question . Is there
I have a question related to this one . I don't want to do
This is related to my previous question here . I want 4 divs (absolute
[This question is somewhat related to this question , but the answers are not...]
my question is kinda related to this question: Numeric Soft Keyboard on Android BUT,
Related to this SO question , I want to put the data loading in
This question is related to this other question @ SuperUser . I want to
First of all, I looked up this related question in here but the solution

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.