通过价值重复分散或泡泡绘图

发布于 2025-01-23 15:26:34 字数 502 浏览 1 评论 0原文

我有一组数字序列保存在2D列表中。列表中的每个元素都是不同长度的子列表,例如1-10范围内的数字。这样:

Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]…]

是否有一种方法可以通过使用matplotlib绘制这些数据散布或气泡绘制的绘图?列表中的每个元素都占X轴上的位置,并且该元素中的所有值均分布在相应的Y轴位置,并且重复值的次数越多,绘制的尺寸或深色越大观点。

我已经知道如何使用matplotlib图散点图,但是我不知道如何一个y轴上绘制一个y轴上的项目。

谢谢。

I have a set of sequences of numbers that are kept in a 2D list. Each element in the list is a sublist of varying lengths, say numbers in the range 1-10. Like this:

Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]…]

Is there a way to draw plot these datas in scatter or bubble plotting with value duplication by use matplotlib? Each element in the list occupies a position on the X-axis, and all the values in the element are distributed in the corresponding Y-axis position, and the more times the value is repeated, the larger the size or dark color of the drawn point.

I already know how to use matplotlib plot scatter plotting, but I don't know how to plot a 2D list item on one Y-axis one by one.

Thank you.

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅黛梨妆こ 2025-01-30 15:26:34

您只需在for循环中绘制每个子列表:

import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]]
plt.figure()
for i, j in enumerate(Lst):
    occurences, sizes = list(zip(*list(Counter(j).items())))
    plt.scatter(i*np.ones(len(occurences))+1, occurences, s=np.array(sizes)*50)

输出:

编辑:满足要点也变得更黑暗的请求。使用此处的答案:变暗或变暗或照亮matplotlib的颜色

import matplotlib.pyplot as plt
from collections import Counter
import numpy as np

def lighten_color(color, amount=0.5):
    """
    Lightens the given color by multiplying (1-luminosity) by the given amount.
    Input can be matplotlib color string, hex string, or RGB tuple.

    Examples:
    >> lighten_color('g', 0.3)
    >> lighten_color('#F034A3', 0.6)
    >> lighten_color((.3,.55,.1), 0.5)
    """
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])

Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]]
occurences, sizes = list(zip(*[list(zip(*list(Counter(j).items()))) for j in Lst]))
maximum = max(max(i) for i in sizes)

plt.figure()
for i, (j, k) in enumerate(zip(occurences, sizes)):
    plt.scatter(i*np.ones(len(j))+1, j, s=np.array(k)*50, color=[lighten_color('b', 2*m/maximum) for m in k])

输出 :

You can just plot each sublist in a for loop:

import matplotlib.pyplot as plt
from collections import Counter
import numpy as np
Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]]
plt.figure()
for i, j in enumerate(Lst):
    occurences, sizes = list(zip(*list(Counter(j).items())))
    plt.scatter(i*np.ones(len(occurences))+1, occurences, s=np.array(sizes)*50)

Output:
enter image description here

Edit: Fulfilling request for points to also become darker. Using the answer from here: Darken or lighten a color in matplotlib

import matplotlib.pyplot as plt
from collections import Counter
import numpy as np

def lighten_color(color, amount=0.5):
    """
    Lightens the given color by multiplying (1-luminosity) by the given amount.
    Input can be matplotlib color string, hex string, or RGB tuple.

    Examples:
    >> lighten_color('g', 0.3)
    >> lighten_color('#F034A3', 0.6)
    >> lighten_color((.3,.55,.1), 0.5)
    """
    import matplotlib.colors as mc
    import colorsys
    try:
        c = mc.cnames[color]
    except:
        c = color
    c = colorsys.rgb_to_hls(*mc.to_rgb(c))
    return colorsys.hls_to_rgb(c[0], 1 - amount * (1 - c[1]), c[2])

Lst = [[1,3,4,4,4,5],[2,7,2,3],[6,5,4,2,4],[2,4,5,7,5,4,2],[4,9,4,1,4,5,4]]
occurences, sizes = list(zip(*[list(zip(*list(Counter(j).items()))) for j in Lst]))
maximum = max(max(i) for i in sizes)

plt.figure()
for i, (j, k) in enumerate(zip(occurences, sizes)):
    plt.scatter(i*np.ones(len(j))+1, j, s=np.array(k)*50, color=[lighten_color('b', 2*m/maximum) for m in k])

Output:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文