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

时间:2015-10-20 15:16:17

标签: pandas matplotlib plot

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

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

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

给我一​​个这样的数字: enter image description here

如何获取colorbar实例以便操作它,例如更改标签或设置滴答?

2 个答案:

答案 0 :(得分:11)

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

1,让我们得到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,这个数字有多少轴?

In [62]:

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

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

In [63]:

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

4,因此,第二轴是彩条轴

In [64]:

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

答案 1 :(得分:1)

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

var theNewObservable = my.observable.things
          //Preprocess this so that `selectMany` will use 
          //dataToSend as the request object
          .map(function(thing) { return generateMyJavascriptObjectFrom(thing); })
          .selectMany(function(dataToSend) {
            var promise = $.ajax({
                type: 'POST',
                url: http://somewhere.com,
                data: dataToSend
            }).promise();

            //Rewrap this into a promise that RxJS can handle
            return promise.then(function(data, status, jqXHR) {
              return {data : data, status : status, jqXHR : jqXHR};
            });
          }, function(request, response) {
            return {
                    infoFromServer : response.jqXHR.getResponse('custom-header'),
                    dataToSend : request
                   };
          });

theNewObservable.subscribe(
  function(combinedInfo) { 
    console.log(combinedInfo) 
  },
  function(err) {
    alert('PC LOAD LETTER!');
    console.error(err);
  });