【问题标题】:How to add scatter to figure over subplots?如何在子图中添加散点图?
【发布时间】:2020-07-28 00:35:21
【问题描述】:

我有一个子图脚本,我添加了子图文本和箭头(脚本结尾)。如何添加散点图?我试过了:

scatter = fig.add_scatter(0.5, 0.5, marker = '_', s=300)
fig.add_scatter(0.5, 0.5, marker = '_', s=300)

错误是: 'Figure' 对象没有属性 'add_scatter'

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D                 # 3d graph
from mpl_toolkits.mplot3d import proj3d                 # 3d graph
from matplotlib.text import Text 
from matplotlib.patches import Arrow 

# Plot subplot 
fig, (ax1, ax2, ax3, ax4) = plt.subplots(4, 2, figsize=(10,13))

x = [0, 1]
y = [0, 5]
fig1 = plt.subplot(421)
plt.plot(x,y)

fig2 = plt.subplot(422)
plt.plot(x,y)

fig3 = plt.subplot(423, projection = '3d') # number of vertical figures, number of horizontal figures, order

# Plot figure with size
fig4=plt.subplot(424, projection = '3d')

fig5=plt.subplot(425, projection = '3d')

fig6=plt.subplot(426, projection = '3d')

fig7=plt.subplot(427, projection = '3d')

fig8=plt.subplot(428, projection = '3d')


text = fig.add_artist(Text(0.56,0.198, text='Hello'))

arrow = fig.add_artist(Arrow(0.52, 0.6, 0, -0.08, width=.02)) 

plt.show()

期望的结果是添加散点图,例如,在第 3 和第 4 个子图之间。

【问题讨论】:

    标签: python-3.x matplotlib figure subplot


    【解决方案1】:

    您可能不了解问题的全部意图,但您可以使用以下代码绘制散点图。

    N=100
    X = np.random.rand(N, 3)
    y = np.random.rand(N) * 2 - 1
    fig3.scatter(X[:, 0], X[:, 1],zs=X[:, 2],zdir='z', s=50, c=y, cmap=plt.cm.jet)
    

    【讨论】:

    • 您的脚本将散点图添加到第三个数字,但我的问题是关于在我的问题中的脚本中添加散点图作为箭头和文本“你好”。
    猜你喜欢
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-14
    • 1970-01-01
    • 2020-10-05
    • 2019-09-23
    相关资源
    最近更新 更多