[PYTHON] Notes on what to do when matplotlib scatter () / scatter3d () does not work

When creating a 3D scatter plot, when passing a list of color scheme values to scatter (), the hidden surface elimination goes wrong.

First conclusion: I gave up passing the spray data as a list and scatter () one by one to solve the problem.


When using the scatter plot drawing method scatter () I usually want to pass the marker position and color coding value in a list.

List xs,ys,zs,When passing vs


ax.scatter(xs, ys, zs, c=vs, norm=norm, s=600, marker='o', alpha=1.0, zdir='z', depthshade=False )

However, for some reason, as soon as I passed the color scheme with c = vs, the hidden surface erasure went crazy. It happens that the marker in the foreground is hidden behind the marker in the back and cannot be seen. (Maybe it depends on the version.)


Action: As a result of trying everything I can think of It was normal when I set "scatter () one marker at a time in the for statement".

Scatter for each one()


ax = fig.add_subplot( 122, projection='3d')
for x,y,z,v in zip(xs,ys,zs,vs):
    ax.scatter(x, y, z, c=(v,),norm=norm, s=600, marker='o', alpha=1.0, zdir='z', depthshade=False )

The following is an example:

Erasing comparison


def plot():
    def randrange(n, vmin, vmax):
        return (vmax - vmin)*np.random.rand(n) + vmin

    n = 20
    xs = randrange(n, 0, 100)
    ys = randrange(n, 0, 100)
    zs = randrange(n, 0, 100)
    vs = xs
    norm = Normalize(vmin=min(vs), vmax=max(vs) )
    
    fig = plt.figure()
    ax = fig.add_subplot(121, projection='3d')
    ax.scatter(xs, ys, zs, c=vs, norm=norm, s=600, marker='o', alpha=1.0, zdir='z', depthshade=False )
    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')
    
    ax = fig.add_subplot( 122, projection='3d')
    for x,y,z,v in zip(xs,ys,zs,vs):
        ax.scatter(x, y, z, c=(v,),norm=norm, s=600, marker='o', alpha=1.0, zdir='z', depthshade=False )

    ax.set_xlabel('X')
    ax.set_ylabel('Y')
    ax.set_zlabel('Z')

    plt.show()

Python3.8 execution result ↓ 3DPlotColoring_compare.png

↑ When the left figure is passed as a list, and the right figure is passed one by one. (I intended to draw in a direction that is easy to distinguish)

The figure on the right is normal.

important point:

    1. When calling scatter () for each marker, in order to unify the coloring range Fix the range with norm = Normalize and pass it to scatter () with norm = norm.
  1. The scatter () option "depthshade = True" is very slow. It seems to be True by default, so be sure to set "depth shade = False".

Recommended Posts

Notes on what to do when matplotlib scatter () / scatter3d () does not work
What to do when Japanese is not displayed on matplotlib
What to do when python3 type venv does not work well on Raspberry Pi
Example of what to do when the sample script does not work (OpenCV-Python)
What to do when the graph does not appear in jupyter (ipython) notebook
[EC2] What to do when selenium is stuck and processing does not proceed
What to do if sys / cdefs.h does not exist
What to do when Python starts up in Anaconda does not come out unexpectedly
What to do when "Type Error: must be _socket.socket, not socket" appears on GAE
What to do if `pip install matplotlib` fails on Mac
What to do if Jupyter Notebook on WSL does not start automatically in your browser
What to do when Ubuntu crashes
LocateCenterOnScreen does not work on PyAutoGui
What to do to get tensorflow-gpu to work
What to do if Python doesn't work on Git for Windows
What to do when the jupyterlab extension settings are not reflected
When the program pip installed on Mac / Marvericks does not work
[Beginner] What to do when "[Errno 2] File b'test.csv' does not exist: b'test.csv" is displayed when reading pandas csv
[virtualbox] What to do when [Could not retrieve mirrorlist] appears when yum update is performed on CentOS7
[VSCode] unable to import'google.cloud' What to do when pylint (import-error) does not recognize the Python import statement
Virtualenv does not work on Python3.5 (Windows)
What to do if pyenv install does not proceed with an error
What to do if you get "The session could not be opened" when installing CentOS on VirtualBox
[Mac OS] What to do when Python is not installed as a framework. Is displayed when import matplotlib is performed.
What to do when you get angry that libxml / xmlversion.h does not exist when you put lxml with pip
I managed to solve the situation where Python does not work on Mac
What to do if CERTIFICATE_VERIFY_FAILED occurs when nltk.download () is done on macOS pyhon
What to do when "TypeError: must be string, not int…" appears when using strptime
What to do if rails s doesn't work
What to do if scrapy doesn't work after installing scrapy with pip on mac
Jinja2 2.9.6 does not work on Lambda Python 3 series
What to do when "TypeError: data type not understood" appears in python's numpy.zeros
What to do if atom autocomplete-python doesn't work
When wildcard specification does not work with pylint
What to do if Japanese language support is not completely installed on Ubuntu 16.04
What to do when matplotlib gets angry on CentOS saying "I'm using Agg so I can't issue a figure"
What to do if you get the error "Error: opencv3: Does not support building both Python 2 and 3 wrappers" when installing openCV 3
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs
What to do if "export" keeps appearing on terminal when trying to put Python on macOS
What to do when is not in the sudoers file.This incident will be reported.
[Pyhton] I want to solve the problem that tkinter does not work on MacOS11
What to do if you get an error when importing matplotlib in Python (Mac)
What to do if Python IntelliSense is not displayed in VS Code on Windows
What to do if you get an Import Error when importing matplotlib with Jupyter
What to do if Python does not switch from the System version in pyenv
What to do if you get the error ʻERR_FEATURE_UNAVAILABLE_ON_PLATFORM` when using ts-node-dev on Linux
[Go 1.13] What to do when unexpected directory layout: appears
[openpyxl] What to do when IllegalCharacterError appears in pandas.DataFrame.to_excel
curl: (60) What to do when Issuer certificate is invalid.
What to do when gdal_merge creates a huge file
Command when ACPI shutdown does not work in VirtualBox
What to do when "cannot import name xxx" [Python]
What to do if pyenv is not enabled (zsh)
What to do when you can't bind CaboCha to Python
[Small story] What to do if "Error with child process: Building resource'awsToolkitSamLocalResource'" does not work when Lambda is locally executed by SAM from VS Code
Parallel processing of Python joblib does not work in uWSGI environment. How to process in parallel on uWSGI?
What to do when you get an error saying "Name resolution temporarily failed" on linux
What to do if the image is not displayed using matplotlib etc. in the Docker container
What to do when a Missing artifact occurs in a jar that is not defined in pom.xml
What to do when no display name occurs when unittesting Python + Tkinter on Github Actions Memo
[AWS] What to do when you want to pip with Lambda