在python中编辑标记形状 [英] Edit marker shape in python

查看:50
本文介绍了在python中编辑标记形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 CDF 图的 x 轴上使用菱形指针来显示一些数据的分布.由于数据数量较多,这些点靠得很近,无法区分.我想知道是否有办法使散点图的菱形标记更.

I'm using diamond pointer on the x-axis of a CDF plot to show the distribution of some data. As the number of data is high, these points are close together and not distinguishable. I was wondering if there is a way to make the diamond marker for scatter plot more pointy.

推荐答案

虽然我喜欢 @Stef 创建新标记符号的答案,但您也可以根据现有符号与其他点的距离调整它们的大小:

While I like @Stef's answer of creating new marker symbols, you can also just adjust the size of existing symbols with regard to their distance to other points:

import matplotlib.pyplot as plt
import numpy as np
from sklearn.neighbors import NearestNeighbors

# create random data
x = np.random.rand(10)
y = np.ones(len(x))

# open figure + axes
fig,axs = plt.subplots(1,2)
# standard scatter-plot
MarkerSize = 40
axs[0].scatter(x,y,s=MarkerSize)

# re-arrange data
xy = []
for x1,y1 in zip(x,y):
    xy.append([x1,y1])
# find nearest neighbors to itself (skip the first column because it finds the exact same element, i.e. with zero distance)
dst,idx = NearestNeighbors(n_neighbors=2).fit(xy).kneighbors(xy)
dst = dst[:,1]

# create a vector for the marker-size
S = dst/dst.max()*MarkerSize
# scatter plot with adjusted marker-size
axs[1].scatter(x,y,s=S)

我使用了 scikit-learn 的 sklearn.neighbors.NearestNeighbors() 计算点之间的最小距离,并将其作为缩放因子传递给 matplotlib.pyplot.scatter() 的大小参数 s=.那里scatter() 中标记大小参数的小教程.

I used scikit-learn's sklearn.neighbors.NearestNeighbors() to calculate the smallest distance between points and pass this as a scaling factor to the size-argument s= of matplotlib.pyplot.scatter(). There is a little tutorial for the marker-size argument in scatter().

这篇关于在python中编辑标记形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆