python - "#using-proxy-artist".format(orig_handle) 带饼图(来自 CSV 的数据) Matplotlib

标签 python csv matplotlib

在饼图中添加图例后,我收到了用户警告:

UserWarning: Legend does not support '47036560' instances.
A proxy artist may be used instead.

我想添加图例以显示 csv 文件中已使用的内存状态和可用内存。

csv 文件:

USED;FREE

26932440;47036560

我的代码:

import matplotlib.pyplot as plt
import numpy as np

plt.rcParams['text.color'] = 'k'

data = np.loadtxt('show-flash.csv' ,dtype=bytes, delimiter=';', usecols=(0, 1)).astype(str)
slice = data[1]
labels = data[0]
colors = ['lightskyblue', 'lightcoral']
explode = [0.05, 0]
plt.pie(slice, labels=labels, colors=colors, explode=explode, startangle=90, shadow=True, autopct='%1.1f%%')

plt.title('Show Flash\n(Bytes)')
plt.legend(slice,labels)
plt.show()

我需要这样的输出: enter image description here

最佳答案

您得到的是一条警告,指出您不能使用字符串作为图例句柄。提供给 plt.legend(handles, labels) 的句柄必须是 matplotlib 艺术家。获取这些艺术家的最简单方法是调用 plt.pie()

pie = plt.pie(...)
plt.legend(pie[0], labels)

完整示例:

import io
import matplotlib.pyplot as plt
import numpy as np

d = u"""USED;FREE

26932440;47036560"""

s = io.StringIO(d)

data = np.loadtxt(s ,dtype=bytes, delimiter=';', usecols=(0, 1)).astype(str)

slices = data[1]
labels = data[0]
colors = ['lightskyblue', 'lightcoral']
explode = [0.05, 0]
pie = plt.pie(slices, labels=labels, colors=colors, explode=explode, startangle=90, 
              shadow=True, autopct='%1.1f%%')

plt.title('Show Flash\n(Bytes)')
plt.legend(pie[0],[" ".join(a) for a in zip(labels, slices)])
plt.show()

制作

enter image description here

关于python - "#using-proxy-artist".format(orig_handle) 带饼图(来自 CSV 的数据) Matplotlib,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43994562/

相关文章:

java.sql.SQLException : Row 1 was truncated; it contained more data than there were input columns 异常

Java 读取包含来自不同语言的字符串的文件

python - 如何收紧 'matplotlib' 数字的边界以使其更接近我的轴?

python - 如何优化我的代码,以便能够使用循环在子图中绘制直方图?

python - 从多项式回归(图)获取函数 ("f(x)")

python - 在循环中切片 NumPy 数组

Python 二进制差异

php - 如何 curl Facebook 广告报告

python - Flask - 获得关键字参数 'eventid' 的多个值 - 装饰器

python - 在散点图的工具提示中为每个气泡显示一个标签 (Matplotlib)