English 中文(简体)
2. 在安达斯/马特图布拉采采采石场
原标题:Getting Colorbar instance of scatter plot in pandas/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 )

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

我如何利用肤色来操纵它,例如改变标签或制定标准?

最佳回答

因此,我们必须确定:

查阅<代码>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>

因此,第二轴是彩票。

In [64]:

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

It s not quite the same but you could just plot using 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()

回答原来的问题:你可以制定<代码>colorbar=False并单独生成。 它需要一个“可适用”的物体,即含有色图信息的试剂。 这里存放在<条码>轴心中。 由于cbar = plt.colorbar(......)的回报值,你可进入彩票和ax

import matplotlib.pyplot as plt
from matplotlib.ticker import MultipleLocator
import pandas as pd
import numpy as np

# [ (0,0), (0,1), ..., (9,9) ]
df = pd.DataFrame({ x : np.repeat(range(10), 10),  y : np.tile(range(10), 10)})

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

ax = df.plot(kind= scatter ,
             x= x ,
             y= y ,
             c= score ,
             s=500,
             cmap= RdYlGn ,
             vmin=0,
             vmax=1,
             colorbar=False)
ax.set_xlim([-0.5, 9.5])
ax.set_ylim([-0.5, 9.5])
ax.xaxis.set_major_locator(MultipleLocator(1))
ax.yaxis.set_major_locator(MultipleLocator(1))
cbar = plt.colorbar(ax.collections[0], ax=ax)
cbar.set_ticks([0, 0.5, 1])
cbar.set_ticklabels([ low ,  medium ,  high ])
cbar.ax.set_title( Score , ha= left )

plt.tight_layout()
plt.show()

“Pandas





相关问题
Interactive mode in matplotlib

I want to dynamically update the scatter plot based on the y-axis data received from a socket connection. I used python matplot lib in interactive mode to do this, but during dynamic updation if i ...

Couple of matplotlib newbie doubts

I am just starting to use matplotlib and I have hit upon 2 major roadblocks, which I can t seem to work around from the docs/examples,etc: Here is Python source: #!/usr/bin/python import matplotlib ...

How to disable screen update in matplotlib

I have a loop that is adding a line to a plot on each iteration. Right now this is horribly slow as it seems to redraw the the whole graph each time. Is it possible to disable screen updates for a ...

How to use Matplotlib in Django?

From some examples from the Internet I made the test code below. It works! ... BUT if I reload the page, the pie will draw itself with the same image. Some parts get darker every time I reload the ...

Plotting vector fields in python (matplotlib)

I found this code on http://matplotlib.sourceforge.net/examples/pylab_examples/quiver_demo.html from pylab import * from numpy import ma X,Y = meshgrid( arange(0,2*pi,.2),arange(0,2*pi,.2) ) U = cos(...

Multiple grids on matplotlib

I m making plots in Python and matplotlib, which I found huge and flexible, till now. The only thing I couldn t find how to do, is to make my plot have multiple grids. I ve looked into the ...

热门标签