matplotlib:更改标题和颜色条文本并勾选颜色

时间:2012-03-12 07:10:49

标签: python matplotlib

我想知道如何更改颜色栏中刻度线的颜色以及如何更改图中标题和颜色条的字体颜色。例如,事情显然在temp.png中可见,但在temp2.​​png中不可见:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn

fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')
plt.title('my random fig')
plt.colorbar()

# works fine
plt.savefig('temp.png')
# title and colorbar ticks and text hidden
plt.savefig('temp2.png', facecolor="black", edgecolor="none")

由于

4 个答案:

答案 0 :(得分:24)

这可以通过检查和设置matplotlib中对象处理程序的属性来完成。

我编辑了你的代码并在评论中加了一些解释:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn

fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')

title_obj = plt.title('my random fig') #get the title property handler
plt.getp(title_obj)                    #print out the properties of title
plt.getp(title_obj, 'text')            #print out the 'text' property for title
plt.setp(title_obj, color='r')         #set the color of title to red

axes_obj = plt.getp(cax,'axes')                 #get the axes' property handler
ytl_obj = plt.getp(axes_obj, 'yticklabels')     #get the properties for 
                                                #  yticklabels
plt.getp(ytl_obj)                               #print out a list of properties
                                                #  for yticklabels
plt.setp(ytl_obj, color="r")                    #set the color of yticks to red

plt.setp(plt.getp(axes_obj, 'xticklabels'), color='r') #xticklabels: same

color_bar = plt.colorbar()                            #this one is a little bit
cbytick_obj = plt.getp(color_bar.ax.axes, 'yticklabels')                #tricky
plt.setp(cbytick_obj, color='r')

plt.savefig('temp.png')
plt.savefig('temp2.png', facecolor="black", edgecolor="none")

答案 1 :(得分:7)

之前的回答没有给出我想要的东西。 我就这样做了:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn
data = np.clip(randn(250,250),-1,1)
data = np.ma.masked_where(data > 0.5, data)


fig, ax1 = plt.subplots(1,1)

im = ax1.imshow(data, interpolation='nearest')
cb = plt.colorbar(im)

fg_color = 'white'
bg_color = 'black'

# IMSHOW    
# set title plus title color
ax1.set_title('ax1 title', color=fg_color)

# set figure facecolor
ax1.patch.set_facecolor(bg_color)

# set tick and ticklabel color
im.axes.tick_params(color=fg_color, labelcolor=fg_color)

# set imshow outline
for spine in im.axes.spines.values():
    spine.set_edgecolor(fg_color)    

# COLORBAR
# set colorbar label plus label color
cb.set_label('colorbar label', color=fg_color)

# set colorbar tick color
cb.ax.yaxis.set_tick_params(color=fg_color)

# set colorbar edgecolor 
cb.outline.set_edgecolor(fg_color)

# set colorbar ticklabels
plt.setp(plt.getp(cb.ax.axes, 'yticklabels'), color=fg_color)

fig.patch.set_facecolor(bg_color)    
plt.tight_layout()
plt.show()
#plt.savefig('save/to/pic.png', dpi=200, facecolor=bg_color)

black on white imshow

答案 2 :(得分:2)

根据之前的回答,我添加了两行来设置颜色条的框颜色和颜色条的刻度颜色:

import matplotlib.pyplot as plt
import numpy as np
from numpy.random import randn

fig = plt.figure()
data = np.clip(randn(250,250),-1,1)
cax = plt.imshow(data, interpolation='nearest')

title_obj = plt.title('my random fig') #get the title property handler
plt.setp(title_obj, color='w')         #set the color of title to white

axes_obj = plt.getp(cax,'axes')                        #get the axes' property handler
plt.setp(plt.getp(axes_obj, 'yticklabels'), color='w') #set yticklabels color
plt.setp(plt.getp(axes_obj, 'xticklabels'), color='w') #set xticklabels color

color_bar = plt.colorbar()                            
plt.setp(plt.getp(color_bar.ax.axes, 'yticklabels'), color='w') # set colorbar  
                                                                # yticklabels color
##### two new lines ####
color_bar.outline.set_color('w')                   #set colorbar box color
color_bar.ax.yaxis.set_tick_params(color='w')      #set colorbar ticks color 
##### two new lines ####

plt.setp(cbytick_obj, color='r')
plt.savefig('temp.png')
plt.savefig('temp3.png', facecolor="black", edgecolor="none")

答案 3 :(得分:0)

虽然其他答案肯定是正确的,但似乎使用样式或特定rcParams或使用tick_params函数

更容易解决

样式

Matplotlib提供dark_background样式。你可以使用它,例如通过plt.style.use("dark_background")

import matplotlib.pyplot as plt
import numpy as np

plt.style.use("dark_background")

fig = plt.figure()
data = np.clip(np.random.randn(150,150),-1,1)
plt.imshow(data)
plt.title('my random fig')
plt.colorbar()  

plt.savefig('temp2.png', facecolor="black", edgecolor="none")
plt.show()

enter image description here

或者,如果您需要创建具有和不具有黑色背景样式的相同图形,则可以在上下文中使用。

import matplotlib.pyplot as plt
import numpy as np

def create_plot():
    fig = plt.figure()
    data = np.clip(np.random.randn(150,150),-1,1)
    plt.imshow(data)
    plt.title('my random fig')
    plt.colorbar()
    return fig

# create white background plot
create_plot()
plt.savefig('white_bg.png')

with plt.style.context("dark_background"):
    create_plot()
    plt.savefig('dark_bg.png', facecolor="black", edgecolor="none")

Customizing matplotlib教程中了解详情。

具体的rcParams

您可以单独设置构成脚本中所需样式的所需rcParams

E.g。使任何文字变成蓝色和红色:

params = {"text.color" : "blue",
          "xtick.color" : "crimson",
          "ytick.color" : "crimson"}
plt.rcParams.update(params)

这将自动为滴答声着色。

enter image description here

自定义刻度和标签

您也可以单独自定义绘图中的对象。对于刻度线和刻度标签,有tick_params方法。例如。只使颜色条的刻度变红,

cbar = plt.colorbar()
cbar.ax.tick_params(color="red", width=5, length=10)

enter image description here