在 pandas/matplotlib 中获取散点图的 Colorbar 实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33240427/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:05:07  来源:igfitidea点击:

Getting Colorbar instance of scatter plot in pandas/matplotlib

pandasmatplotlibplot

提问by desiato

How do I get the internally created colorbar instance of a plot created by pandas.DataFrame.plot?

如何获取由 pandas.DataFrame.plot 创建的绘图的内部创建的颜色条实例?

Here is an example for generating a colored scatter plot:

以下是生成彩色散点图的示例:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import itertools as it

# [ (0,0), (0,1), ..., (9,9) ]
xy_positions = list( it.product( range(10), range(10) ) )

df = pd.DataFrame( xy_positions, columns=['x','y'] )

# draw 100 floats
df['score'] = np.random.random( 100 )

ax = df.plot( kind='scatter',
              x='x',
              y='y',
              c='score',
              s=500)
ax.set_xlim( [-0.5,9.5] )
ax.set_ylim( [-0.5,9.5] )

plt.show()

which gives me a figure like this: enter image description here

这给了我一个这样的数字: 在此处输入图片说明

How do I get the colorbar instance in order to manipulate it, for instance for changing the label or setting the ticks?

如何获取颜色条实例以对其进行操作,例如更改标签或设置刻度?

回答by CT Zhu

pandasdoes not return the axis for the colorbar, therefore we have to locate it:

pandas不返回颜色条的轴,因此我们必须找到它:

1st, let's get the figureinstance: i.e., use plt.gcf()

第一,让我们得到figure实例:即,使用plt.gcf()

In [61]:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import itertools as it

# [ (0,0), (0,1), ..., (9,9) ]
xy_positions = list( it.product( range(10), range(10) ) )

df = pd.DataFrame( xy_positions, columns=['x','y'] )

# draw 100 floats
df['score'] = np.random.random( 100 )

ax = df.plot( kind='scatter',
              x='x',
              y='y',
              c='score',
              s=500)
ax.set_xlim( [-0.5,9.5] )
ax.set_ylim( [-0.5,9.5] )

f = plt.gcf()

2, how many axes does this figure have?

2、这个图形有几根轴?

In [62]:

f.get_axes()
Out[62]:
[<matplotlib.axes._subplots.AxesSubplot at 0x120a4d450>,
 <matplotlib.axes._subplots.AxesSubplot at 0x120ad0050>]

3, The first axes (that is, the first one created), contains the plot

3、第一个轴(即创建的第一个轴),包含绘图

In [63]:

ax
Out[63]:
<matplotlib.axes._subplots.AxesSubplot at 0x120a4d450>

4, Therefore, the second axis is the colorbar axes

4,因此,第二个轴是颜色条轴

In [64]:

cax = f.get_axes()[1]
#and we can modify it, i.e.:
cax.set_ylabel('test')

回答by matt_s

It's not quite the same but you could just plot using matplotlib:

它并不完全相同,但您可以使用 matplotlib 进行绘图:

import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import itertools as it

# [ (0,0), (0,1), ..., (9,9) ]
xy_positions = list( it.product( range(10), range(10) ) )

df = pd.DataFrame( xy_positions, columns=['x','y'] )

# draw 100 floats
df['score'] = np.random.random( 100 )

fig = plt.figure()
ax = fig.add_subplot(111)

s = ax.scatter(df.x, df.y, c=df.score, s=500)
cb = plt.colorbar(s)
cb.set_label('desired_label')

ax.set_xlim( [-0.5,9.5] )
ax.set_ylim( [-0.5,9.5] )

plt.show()