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

  • Home
  • SEARCH
  • 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 6745375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:11:50+00:00 2026-05-26T12:11:50+00:00

I have a matplotlib figure that I want to be able to switch between

  • 0

I have a matplotlib figure that I want to be able to switch between 2D and 3D projections. I can go from 2D to 3D but I can’t seem to work out how to go the other way. Example…

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

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

fig = plt.figure()

# Create a 3D scatter plot...
ax = fig.add_subplot(111, projection='3d')
n = 100
for c, m, zl, zh in [('r', 'o', -50, -25), ('b', '^', -30, -5)]:
    xs = randrange(n, 23, 32)
    ys = randrange(n, 0, 100)
    zs = randrange(n, zl, zh)
    ax.scatter(xs, ys, zs, c=c, marker=m)

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

# Now I want a 2D plot...
ax.cla()
ax = fig.add_subplot(111)
ax.plot(xs, ys)

plt.show()

The plot stays in the 3D projection and projection="2D" isn’t a valid kwarg…

I thought perhaps ax.clf() would do what I wanted and let me define a new figure. But it just gives me the following error:
ValueError: Unknown element o

Can anyone give me a hint as to the solution to this? Is the ValueError related to the problem or a hint to something else wrong with my setup? Is there a kwarg to switch the projection from 3D to 2D?

  • 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-26T12:11:50+00:00Added an answer on May 26, 2026 at 12:11 pm

    I believe I have found one possible solution, although it seems to result in a bit of a memory issue. I suspect that it isn’t actually deleting the initial plot data, just removing it from the figure so memory usage does climb every time the projection is changed.

    # Delete the 3D subplot
    self.fig.delaxes(self.axes)
    # Create a new subplot that is 2D
    self.axes = self.fig.add_subplot(111)
    # 2D scatter
    self.axes.plot(10*np.random.randn(100), 10*np.random.randn(100), 'o')
    # Update the figure
    self.canvas.draw()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that has an output() method which returns a matplotlib Figure
Say that I have two figures in matplotlib, with one plot per figure: import
I am trying to make a matplotlib figure that will have multiple horizontal boxplots
Is there a way to save a Matplotlib figure such that it can be
I am playing with matplotlib - I have a bar chart, and I want
I have two subplots in a figure. I want to set the axes of
I have a very simple basic bar's graphic like this one but i want
I have a matplotlib figure inside of a wxmpl panel, and am trying to
I have a Python program that generates a histogram using matplotlib. The problem is
I have a matplotlib figure on a canvas on a wxpython panel. My application

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.