<图大小 800×800,无坐标轴> 在使用 plt.show() 时,Jupyter Notebook

huangapple go评论73阅读模式
英文:

<Figure size 800x800 with 0 Axes> when using plt.show() Jupyter Notebook

问题

以下是您要翻译的内容:

我正在尝试参加CISCO IoT Big Data课程,我必须在Jupyter Notebook中使用Python内核进行实验。尽管我已经完全按照实验中显示的方式操作,但我仍然看不到图表。它只是显示:<Figure size 800x800 with 0 Axes>
以下是代码:

%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact

def scatter_view(x, y, z, azim, elev):
    # 初始化图和坐标轴
    fig = plt.figure(figsize=(8, 8))
    ax = Axes3D(fig)
    
    # 计算散点图
    ax.scatter(x, y, z)
    ax.set_xlabel('D rate (Mbit/s)', fontsize=16)
    ax.set_ylabel('U rate (Mbit/s)', fontsize=16)
    ax.set_zlabel('P rate (1/s)', fontsize=16)
    ax.azim = azim
    ax.elev = elev

xi = df_rates['download_rate']
yi = df_rates['upload_rate']
zi = df_rates['ping_rate']
interact(lambda azim, elev: scatter_view(xi, yi, zi, azim, elev),
         azim=(0, 90), elev=(0, 90))
plt.show()

我已经尝试更改图形大小,在导入之前或之后使用matplotlib。我在互联网上找不到解决方案。我对这个概念非常陌生,如果有人能解释为什么它不显示,我将不胜感激。谢谢!

英文:

I am trying to do a CISCO IoT Big Data course and i have to do a lab in jupyter notebook with Python kernel. Still i can't see the figure even if i did exactly like it shows me in the lab. It simply shows me: &lt;Figure size 800x800 with 0 Axes&gt;
Here is the code:


%matplotlib inline
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from ipywidgets import interact

def scatter_view(x, y, z, azim, elev):
    # Init figure and axes
    fig = plt.figure(figsize=(8, 8))
    ax = Axes3D(fig)
    
    # Compute scatter plot
    ax.scatter(x, y, z)
    ax.set_xlabel(&#39;D rate (Mbit/s)&#39;, fontsize=16)
    ax.set_ylabel(&#39;U rate (Mbit/s)&#39;, fontsize=16)
    ax.set_zlabel(&#39;P rate (1/s)&#39;, fontsize=16)
    ax.azim = azim
    ax.elev = elev

xi = df_rates[&#39;download_rate&#39;]
yi = df_rates[&#39;upload_rate&#39;]
zi = df_rates[&#39;ping_rate&#39;]
interact(lambda azim, elev: scatter_view(xi, yi, zi, azim, elev),
         azim=(0, 90), elev=(0, 90))
plt.show()

I have tried to change figure size, use matplotlib in line before or after the import. I couldn't find a solution on the internet. I'm pretty new to this concept, if anyone can explain to me why it doesn't show. Thanks!

答案1

得分: 0

Replace the line

ax = Axes3D(fig)

with

ax = fig.add_subplot(projection='3d')

See the documentation:

3D Axes (of class Axes3D) are created by passing the projection="3d" keyword argument to Figure.add_subplot:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection='3d')

Also quoting the api documentation for Axis3D:

Note

As a user, you do not instantiate Axes directly, but use Axes creation methods instead; e.g. from pyplot or Figure: subplots, subplot_mosaic or Figure.add_axes.

英文:

Replace the line

ax = Axes3D(fig)

with

ax = fig.add_subplot(projection=&#39;3d&#39;)

See the documentation:
> 3D Axes (of class Axes3D) are created by passing the projection="3d" keyword argument to Figure.add_subplot:

import matplotlib.pyplot as plt
fig = plt.figure()
ax = fig.add_subplot(projection=&#39;3d&#39;)

Also quoting the api documentation for Axis3D:
> Note
>
> As a user, you do not instantiate Axes directly, but use Axes creation methods instead; e.g. from pyplot or Figure: subplots, subplot_mosaic or Figure.add_axes.

huangapple
  • 本文由 发表于 2023年5月10日 18:39:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76217413.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定