【问题标题】:Changing colorbar of pandas plot改变熊猫情节的颜色条
【发布时间】:2017-11-02 19:31:39
【问题描述】:

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

ax = df1.plot(x='Lat', y='Lon', kind='scatter', c='Thickness')

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

我知道colorbar方法set_label,但是我不知道如何从pandas的@​​987654331@函数返回的ax访问colorbar对象。

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


为了澄清,我添加了图表的图片。我有兴趣更改颜色条的标签。

【问题讨论】:

  • Thickness 参数是分类的吗?
  • 不,数据由数字组成。所有双打。
  • 所以你想把'Thickness'改成别的,对吧?
  • 尝试使用set_label,如this example
  • 我想我找到了the answer you're looking for

标签: python pandas matplotlib plot dataframe


【解决方案1】:

使用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()

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

【讨论】:

猜你喜欢
  • 2023-03-17
  • 2021-08-15
  • 2018-09-06
  • 2020-02-14
  • 2019-01-03
  • 2020-09-20
  • 2014-06-01
  • 2015-12-08
相关资源
最近更新 更多