我有以下代码可以生成 3D 曲面图:
import numpy as np
import matplotlib.pyplot as plt
# Creating a figure object
surf_3D = plt.figure()
# Creating a axes object
axes_3d = plt.axes(projection = "3d")
# Populating X and y
x_demo3d = np.arange(-5,5,0.1)
y_demo3d = np.arange(-5,5,0.1)
# Creating Mesh with newly generated numbers
X3,Y3 = np.meshgrid(x_demo3d, y_demo3d)
# Populating Z with sin and cos function
Z3 = np.sin(X3) * np.cos(Y3)
# Adding
axes_3d.plot_surface(X3, Y3, Z3, cmap = "plasma")
# Making the panes transparent
axes_3d.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axes_3d.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axes_3d.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# Hiding the axes ticks
axes_3d.set_xticks([])
axes_3d.set_yticks([])
axes_3d.set_zticks([])
# Displaying the figure object
surf_3D.show()
输出如下:
将图形放置在画布上后如何更改画布背景?正如你所看到的,我用背景颜色定义了画布的宽度和高度。但显然,我的人物占据了画布的所有空间。是否可以显示未占用的画布空间的背景颜色?
import numpy as np
import matplotlib.pyplot as plt
from tkinter import *
from matplotlib.backends.backend_tkagg import (
FigureCanvasTkAgg, NavigationToolbar2Tk)
class Program_Class:
def __init__(self, master):
# Main Window parameters
Main.resizable(0,0)
Main.geometry("900x550")
Main.title("Main")
# Creating a figure object
surf_3D = plt.figure(figsize=(2,2))
# Creating axes object
axes_3d = plt.axes(projection = "3d")
# Populating with x and y list variables
x = np.arange(-5,5,0.1)
y = np.arange(-5,5,0.1)
# Creating Mesh and rewriting list variables x and y
x, y = np.meshgrid(x, y)
# Populating z with sin and cos function
z = np.sin(x) * np.cos(y)
# Creating the surface 3D plot
axes_3d.plot_surface(x, y, z, cmap = "plasma")
# Making the panes transparent
axes_3d.xaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axes_3d.yaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
axes_3d.zaxis.set_pane_color((1.0, 1.0, 1.0, 0.0))
# Hiding the axes ticks
axes_3d.set_xticks([])
axes_3d.set_yticks([])
axes_3d.set_zticks([])
# My attempt to draw the canvas with the surf_3D figure object
# Canvas background color should be dark red (139, 0, 0)
Canvas_demo3d = Canvas(Main, width=300, height =200, borderwidth=0, highlightthickness=0,
bg='#%02x%02x%02x' % (139, 0, 0))
Canvas_demo3d = FigureCanvasTkAgg(surf_3D, master=Main)
Canvas_demo3d.get_tk_widget().place(x=300,y=71)
Canvas_demo3d.draw()
Main = Tk()
Program_Class(Main)
Main.mainloop()
这是类输出:
任何帮助,我们将不胜感激。
请注意,
Canvas_demo3d = Canvas(...)
是无用的,因为它被行 Canvas_demo3d = FigureCanvasTkAgg(...)
覆盖。
您可以通过以下方式设置画布的背景颜色:
color = '#%02x%02x%02x' % (139, 0, 0)
Canvas_demo3d.get_tk_widget().config(bg=color)
但是为了看到画布背景颜色,您需要使绘图背景透明:
transparent = (0, 0, 0, 0)
# Creating a figure object
surf_3D = plt.figure(figsize=(2,2), facecolor=transparent)
# Creating axes object
axes_3d = plt.axes(projection="3d", facecolor=transparent)
结果: