【问题标题】:matplotlib separating scatterplot points and creating a divisionary curvematplotlib 分离散点图并创建分割曲线
【发布时间】:2019-01-28 19:37:48
【问题描述】:

我正在尝试在 matplotlib 中的散点图上创建一条分割曲线,该曲线将根据标记大小划分我的散点图。

(x,y) 是 phi0 和 phi0dot,我正在根据第三个变量“e-folds”进行着色/调整大小。我想绘制一条“S”形曲线,将绘图分成小的黑色标记和大的青色标记。

Here 是一个示例散点图,其中的点数非常少。最终,我将运行数万个数据点,这样分区会更精细,更明显的“S”形。 This 大致就是我的想法。

到目前为止,我的代码如下所示:

# Set up the PDF
pdf_pages = PdfPages(outfile)
plt.rcParams["font.family"] = "serif"

# Create the canvas
canvas = plt.figure(figsize=(14.0, 14.0), dpi=100)
plt.subplot(1, 1, 1)

for a, phi0, phi0dot, efolds in datastore:
    if efolds[-1] > 65:
        plt.scatter(phi0[0], phi0dot[0], s=200, color='aqua')
    else:
        plt.scatter(phi0[0], phi0dot[0], s=30, color='black')

# Apply labels
plt.xlabel(r"$\phi_0$")
plt.ylabel(r"$\dot{\phi}_0$")

# Finish the file
pdf_pages.savefig(canvas)
pdf_pages.close()
print("Finished!")

This type of separation 非常类似于我想做的事情,但没有立即看到我将如何将其扩展到我的问题。任何建议将不胜感激。

【问题讨论】:

  • 情节是否总是垂直(大致)拆分
  • 或多或少——它通常呈现出我画的形状,但没有向右顶部的切口。
  • 请包含足够的示例数据以用于测试。 - minimal reproducible example
  • 我正在运行 12 小时以收集数万点散点图的数据,目前无法提供数据。但是,如果您对如何在理论上使用具有明确划分点的任意散点图绘制这条线有任何想法,我很想听听您的想法。我的脚本完成后将上传此玩具模型的数据。

标签: python matplotlib


【解决方案1】:

我会假设不同分类点之间的分隔线是沿着阈值的简单等高线。

这里我假设分类采用01 的值,因此可以沿着0.5 绘制contour

ax.contour(x,y,clas, [0.5])

例子:

import numpy as np
import matplotlib.pyplot as plt

# Some data on a grid
x,y = np.meshgrid(np.arange(20), np.arange(10))
z = np.sin(y+1) + 2*np.cos(x/5) + 2


fig, ax = plt.subplots()

# Threshold; values above the threshold belong to another class as those below.
thresh = 2.5
clas = z > thresh
size = 100*clas + 30*~clas

# scatter plot
ax.scatter(x.flatten(), y.flatten(), s = size.flatten(), c=clas.flatten(), cmap="bwr")
# threshold line
ax.contour(x,y,clas, [.5], colors="k", linewidths=2)

plt.show()

【讨论】:

    猜你喜欢
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2013-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多