更改 pandas 图的颜色条

J.A*_*ado 5 python plot matplotlib dataframe pandas

我使用数据框方法绘制了一个图表plot

ax = df1.plot(x='Lat', y='Lon', kind='scatter', c='Thickness')
Run Code Online (Sandbox Code Playgroud)

结果是一个散点图,其中点按 中设置的参数缩放c='Thickness'。图表旁边的颜色条自动接收标签Thickness。我想改变它。

我知道 colorbar 方法set_label,但我不知道如何从axpandasplot函数返回的值访问 colorbar 对象。

如何访问图中的颜色条对象以更改其标签?


为了澄清起见,我添加了图表的图片。我有兴趣更改颜色条的标签。在此输入图像描述

Bel*_*ter 0

pandas用于设置colorbar标签太复杂。可以matplotlib.pyplot直接使用,这是一个例子

import numpy as np
import matplotlib.pyplot as plt

np.random.seed(0)
n = 100000
x = np.random.standard_normal(n)
y = 2.0 + 3.0 * x + 4.0 * np.random.standard_normal(n)
xmin = x.min()
xmax = x.max()
ymin = y.min()
ymax = y.max()

fig, axs = plt.subplots(ncols=2, sharey=True, figsize=(7, 4))
fig.subplots_adjust(hspace=0.5, left=0.07, right=0.93)
ax = axs[0]
hb = ax.hexbin(x, y, gridsize=50, cmap='inferno')
ax.axis([xmin, xmax, ymin, ymax])
ax.set_title("Hexagon binning")
cb = fig.colorbar(hb, ax=ax)
cb.set_label('counts')

ax = axs[1]
hb = ax.hexbin(x, y, gridsize=50, bins='log', cmap='inferno')
ax.axis([xmin, xmax, ymin, ymax])
ax.set_title("With a log color scale")
cb = fig.colorbar(hb, ax=ax)
cb.set_label('log10(N)')

plt.show()
Run Code Online (Sandbox Code Playgroud)

参考: http: //matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.hexbin

  • 它并不太复杂:/sf/ask/2326829921/#33242080 (3认同)