Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为使用plt.fill绘制的绘图创建颜色栏_Python_Matplotlib_Colorbar - Fatal编程技术网

Python 为使用plt.fill绘制的绘图创建颜色栏

Python 为使用plt.fill绘制的绘图创建颜色栏,python,matplotlib,colorbar,Python,Matplotlib,Colorbar,我是Python新手(之前是IDL用户),所以我希望我的问题是可以理解的。我一直在尝试创建一个极坐标图,其中包含x个箱子,箱子中的数据是平均值,并给出与该值相关的颜色。在使用plt.fill命令时,这似乎工作得很好,我可以在其中定义箱子,然后定义填充颜色。当我试着做一个颜色条来搭配它时,问题就来了。我不断收到状态AttributeError:“Figure”对象没有属性“autoscale\u None”的错误 任何建议都会有帮助的,谢谢 import matplotlib.pyplot as

我是Python新手(之前是IDL用户),所以我希望我的问题是可以理解的。我一直在尝试创建一个极坐标图,其中包含x个箱子,箱子中的数据是平均值,并给出与该值相关的颜色。在使用plt.fill命令时,这似乎工作得很好,我可以在其中定义箱子,然后定义填充颜色。当我试着做一个颜色条来搭配它时,问题就来了。我不断收到状态AttributeError:“Figure”对象没有属性“autoscale\u None”的错误

任何建议都会有帮助的,谢谢

import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import matplotlib.cm as cm
from matplotlib.pyplot import figure, show, rc, grid
import pylab

r = np.arange(50)/5.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*2.*np.pi
tstep = theta[1] - theta[0]
colorv = np.arange(50)/50.

# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)

my_cmap = cm.jet
for j in range(len(r)):
    rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep])
    for i in range(len(theta)):
        thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]])
        x = rbox*np.cos(thetabox)
        y = rbox*np.sin(thetabox)
        plt.fill(x,y, facecolor = my_cmap(colorv[j]))



# Add colorbar, make sure to specify tick locations to match desired ticklabels
cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
cb = plt.colorbar()
plt.show()
*这是我真实数据的一个稍微好一点的例子,到处都是漏洞,所以在这个例子中,我在圆的四分之一处做了一个大洞。当我尝试网格划分时,代码似乎试图在这些区域上插值

r = np.arange(50)/50.*7. + 3.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*1.5*np.pi - np.pi
tstep = theta[1] - theta[0]
colorv = np.sin(r/10.*np.pi)

# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)

my_cmap = cm.jet

for j in range(len(r)):
    rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep])
    for i in range(len(theta)):
        thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]])
        x = rbox*np.cos(thetabox)
        y = rbox*np.sin(thetabox)
        plt.fill(x,y, facecolor = my_cmap(colorv[j]))


# Add colorbar, make sure to specify tick locations to match desired ticklabels
#cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
#cb = plt.colorbar()
plt.show()
然后再加上一个啮合

从matplotlib.mlab导入网格数据

r = np.arange(50)/5.
rstep = r[1] - r[0]
theta = np.arange(50)/50.*1.5*np.pi - np.pi
tstep = theta[1] - theta[0]
colorv = np.sin(r/10.*np.pi)

# force square figure and square axes looks better for polar, IMO
width, height = mpl.rcParams['figure.figsize']
size = min(width, height)
# make a square figure
fig = figure(figsize=(size, size))
ax = fig.add_axes([0.1, 0.1, .8, .8])#, polar=True)

my_cmap = cm.jet

x = r*np.cos(theta)
y = r*np.sin(theta)
X,Y = np.meshgrid(x,y)

data = griddata(x,y,colorv,X,Y)
cax = plt.contourf(X,Y, data)
plt.colorbar()

# Add colorbar, make sure to specify tick locations to match desired ticklabels
#cbar = fig.colorbar(fig, ticks=[np.min(colorv), np.max(colorv)])
#cb = plt.colorbar()
plt.show()

colorbar
需要将事物作为
ScalarMappable
的一个实例,以便从它们生成一个colorbar

因为您要手动设置每个磁贴,所以实际上没有任何东西具有颜色条

有很多方法可以从颜色贴图中伪造它,但在这种情况下,有一个更简单的解决方案

pcolormesh
完全满足您的需求,而且速度会更快

例如:

import numpy as np
import matplotlib.pyplot as plt

# Linspace makes what you're doing _much_ easier (and includes endpoints)
r = np.linspace(0, 10, 50)
theta = np.linspace(0, 2*np.pi, 50)

fig = plt.figure()
ax = fig.add_subplot(111, projection='polar')

# "Grid" r and theta into 2D arrays (see the docs for meshgrid)
r, theta = np.meshgrid(r, theta)
cax = ax.pcolormesh(theta, r, r, edgecolors='black', antialiased=True)

# We could just call `plt.colorbar`, but I prefer to be more explicit
# and pass in the artist that I want it to extract colors from.
fig.colorbar(cax)

plt.show()

或者,如果您更喜欢非极轴,如示例代码中所示:

import numpy as np
import matplotlib.pyplot as plt

r = np.linspace(0, 10, 50)
theta = np.linspace(0, 2*np.pi, 50)

# "Grid" r and theta and convert them to cartesian coords...
r, theta = np.meshgrid(r, theta)
x, y = r * np.cos(theta), r * np.sin(theta)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.axis('equal')

cax = ax.pcolormesh(x, y, r, edgecolors='black', antialiased=True)

fig.colorbar(cax)

plt.show()

注意:如果您希望边界线稍微暗一点,只需指定
linewidth=0.5
或类似于
pcolormesh

最后,如果您确实想从原始代码中的colormap直接生成colorbar,您可以从中创建一个
scalarMapable
实例,并将其传递给
colorbar
。这比听起来容易,但有点冗长

例如,在原始代码中,如果执行以下操作:

cax = cm.ScalarMappable(cmap=my_cmap)
cax.set_array(colorv)
fig.colorbar(cax)

它可以做你想做的。

所以我找到了一个解决办法。因为我知道一个区域肯定没有数据,所以我在那里画了一些。我已经确保数据涵盖了我灌封的所有内容。然后我把它盖起来(这个区域无论如何都会被覆盖,它显示了“地球”的位置)。现在我可以继续使用plt.fill,就像我最初使用的一样,使用随机封装数据中的颜色栏。我知道这可能不是正确的方法,但它是有效的,不会试图插入我的数据

非常感谢你帮我整理这件事。如果你知道更好的方法,我很高兴听到

hid = plt.pcolormesh(X,Y, data, antialiased=True)

#here we cover up the region that we just plotted in
r3 = [1 for i in range(360)]
theta3 = np.arange(360)*np.pi/180.
plt.fill(theta3, r3, 'w')

#now we can go through and fill in all the regions
for j in range(len(r)):
    rbox = np.array([r[j], r[j], r[j]+ rstep, r[j] + rstep])
    for i in range(len(theta)):
        thetabox = np.array([theta[i], theta[i] + tstep, theta[i] + tstep, theta[i]])
        x = rbox*np.cos(thetabox)
        y = rbox*np.sin(thetabox)
        colorv = np.sin(r[j]/10.*np.pi)
        plt.fill(thetabox,rbox, facecolor = my_cmap(colorv))
#And now we can plot the color bar that fits the data Tada :)
plt.colorbar()
plt.show()

另一方面,matplotlib命令通常不需要使用
抗锯齿=True
kwarg(这是大多数命令的默认设置)
pcolormesh
出于性能原因,默认情况下不进行抗锯齿,因为网格“单元”通常是垂直的,并且在没有抗锯齿的情况下外观良好。在这种情况下,单元格不是垂直的,性能影响也不算太坏,因此在启用抗锯齿的情况下绘制网格是个好主意。+1,顺便说一句:“有很多方法可以从颜色贴图中伪造它”请,您能给出一些提示/示例吗?我一直在尝试从操作代码中获取颜色条,但没有成功…我曾考虑使用代理艺术家,但我认为可能有更干净的方法。我将添加一个例子。@joaquin-代理艺术家肯定是最难的。我没在想!无论如何,我加了一个例子。希望有帮助@Alexa Halford-
pcolormesh
将正确处理丢失的数据,如果您a)放入NaN或b)使用屏蔽数组。它应该满足你的需要,我想。。。