如何制作嵌套比例面积图(圆圈)? [英] How to do a Nested Proportional Area Chart (circles)?

查看:251
本文介绍了如何制作嵌套比例面积图(圆圈)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找python中可以用来在圈子中绘制嵌套比例面积图的任何内容。最好使用matplotlib构建(或在其之上)。以下是此类绘图的示例供您参考:

I'm looking for anything in python that I can use to do a nested proportional area chart in circles. Preferably something built with (or on top of) matplotlib. Here's an example of what such plot looks like for reference:

推荐答案

一个嵌套的圆形图,其中圆形面积与数据成比例,如下所示。
它将使用排序后的列表或数据数组以及可选的相应标签作为输入,并绘制几个圆。

A nested circle diagram, where the circle area is proportional to the data could look as follows. It would take a sorted list or array of data and optionally the respective labels as input and plot a couple of circles.

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


def nested_circles(data, labels=None, c=None, ax=None, 
                   cmap=None, norm=None, textkw={}):
    ax = ax or plt.gca()
    data = np.array(data)
    R = np.sqrt(data/data.max())
    p = [plt.Circle((0,r), radius=r) for r in R[::-1]]
    arr = data[::-1] if c is None else np.array(c[::-1])
    col = PatchCollection(p, cmap=cmap, norm=norm, array=arr)

    ax.add_collection(col)
    ax.axis("off")
    ax.set_aspect("equal")
    ax.autoscale()

    if labels is not None:
        kw = dict(color="white", va="center", ha="center")
        kw.update(textkw)
        ax.text(0, R[0], labels[0], **kw)
        for i in range(1, len(R)):
            ax.text(0, R[i]+R[i-1], labels[i], **kw)
    return col

用法可能看起来像

data = [1,3,4,5,6]
labels = list("ABCDE")
nested_circles(data, labels=labels, cmap="copper", textkw=dict(fontsize=14))
plt.show()

如果您想要其他颜色编码,请使用 c 参数并提供另一个列表值,例如

If you want a different colorcoding, take the c argument and supply another list of values, e.g.

data = [1,3,4,5,6]
labels = list("ABCDE")
codes = [5,3,1,4,2]
circles = nested_circles(data, labels=labels, c=codes, cmap="plasma", 
                         textkw=dict(color="black", fontsize=14))
plt.colorbar(circles, label="Codes")
plt.title("Diagram")
plt.show()

这篇关于如何制作嵌套比例面积图(圆圈)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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