使用 matplotlib 在图例上添加标签不起作用 [英] Adding labels on legend does not work, using matplotlib

查看:101
本文介绍了使用 matplotlib 在图例上添加标签不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据框

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'var': ['bid', 'on', 'off', 'bid', 'on', 'off'],
                   'aud': ['H', 'H', 'H', 'L', 'L', 'L'],
                   'eff': [0.1, 0.2, 0.3, 0.01, 0.02, 0.03],
                   'spend': [10, 20, 30, 1, 2, 3],
                   'marg': [0.001, 0.002, 0.003, 0.0001, 0.0002, 0.0003]})

我的最终目标是创建一个气泡图,为每个 varmargspecific colors> 在 y_axis 上,图的 x_axis 上的 eff 和气泡的 size 相等使用 colorsize

My end goal is to create a bubble plot, with specific colors for every var, the marg on the y_axis, the eff on the x_axis of the plot and the size of the bubbles to be equal to spend with respective legends for the color and the size

我正在使用以下代码

x = df.loc[df.aud == 'H']['eff']
y = df.loc[df.aud == 'H']['marg']
z = df.loc[df.aud == 'H']['spend']
colours_dict = dict(zip(['bid', 'on', 'off'], ['#185177', '#FAA22C', '#8FC5E8']))

g, ax = plt.subplots()

scatter = ax.scatter(x, y, c=[ colours_dict[i] for i in df.loc[df.aud == 'H']['var'] ], s=z*10)

# produce a legend with the unique colors from the scatter
legend1 = ax.legend(*scatter.legend_elements(),
                    loc="center", title="var")
ax.add_artist(legend1)

# produce a legend with a cross section of sizes from the scatter
handles, labels = scatter.legend_elements(prop="sizes", alpha=0.6)
legend2 = ax.legend(handles, labels, loc="upper right", title="Spend")

plt.savefig('bubbles.png')

问题在于带有颜色的图例没有显示标签.

The problem is that the legend with the colors does not show the labels.

有帮助吗?

推荐答案

您有两个选择:

import pandas as pd
import matplotlib.pyplot as plt

df = pd.DataFrame({'var': ['bid', 'on', 'off', 'bid', 'on', 'off'],
                   'aud': ['H', 'H', 'H', 'L', 'L', 'L'],
                   'eff': [0.1, 0.2, 0.3, 0.01, 0.02, 0.03],
                   'spend': [10, 20, 30, 1, 2, 3],
                   'marg': [0.001, 0.002, 0.003, 0.0001, 0.0002, 0.0003]})

x = df.loc[df.aud == 'H']['eff']
y = df.loc[df.aud == 'H']['marg']
z = df.loc[df.aud == 'H']['spend']
labels = ['bid', 'on', 'off']
colors = ['#185177', '#FAA22C', '#8FC5E8']
colours_dict = dict(zip(labels, colors))

fig, ax = plt.subplots()

c = [colours_dict[i] for i in df.loc[df.aud == 'H']['var']]
scatter = ax.scatter(x, y, c=c, s=z*10)

# produce a legend with the unique colors from the scatter
handles = [plt.Line2D([],[], ls="", marker="o", color=c) for c in colors] 
legend1 = ax.legend(handles, labels, loc="lower right", title="var")

plt.show()

使用颜色映射

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap, BoundaryNorm

df = pd.DataFrame({'var': ['bid', 'on', 'off', 'bid', 'on', 'off'],
                   'aud': ['H', 'H', 'H', 'L', 'L', 'L'],
                   'eff': [0.1, 0.2, 0.3, 0.01, 0.02, 0.03],
                   'spend': [10, 20, 30, 1, 2, 3],
                   'marg': [0.001, 0.002, 0.003, 0.0001, 0.0002, 0.0003]})

x = df.loc[df.aud == 'H']['eff']
y = df.loc[df.aud == 'H']['marg']
z = df.loc[df.aud == 'H']['spend']

labels = ['bid', 'on', 'off',]
colors = ['#185177', '#FAA22C', '#8FC5E8']
inv = [labels.index(i) for i  in df.loc[df.aud == 'H']['var']]

cmap = ListedColormap(colors)
norm = BoundaryNorm(np.arange(len(colors)+1)-0.5, len(colors))

fig, ax = plt.subplots()

scatter = ax.scatter(x, y, c=inv, s=z*10, cmap=cmap, norm=norm)

legend1 = ax.legend(scatter.legend_elements(num=len(labels))[0], labels, 
                    loc="lower right", title="var")

plt.show()

这篇关于使用 matplotlib 在图例上添加标签不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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