matplotlib 更改标题和颜色栏文本以及勾选颜色

zbsbpyhn  于 2023-10-24  发布在  其他
关注(0)|答案(5)|浏览(128)

我想知道如何改变颜色条中的刻度的颜色,以及如何改变图中标题和颜色条的字体颜色。例如,很明显,在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")

谢谢

mgdq6dx1

mgdq6dx11#

以前的回答没有给给予我想要的。这是我是如何做到的:

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)

rnmwe5a2

rnmwe5a22#

(更新:此答案中的信息已过时,请滚动下方以获取最新且更适合新版本的其他答案)

这可以通过在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")
l5tcr1uw

l5tcr1uw3#

虽然其他答案肯定是正确的,但似乎使用样式或特定的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()

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

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,这些rcParams构成脚本中需要的样式。
例如,使任何文本为蓝色,yticks为红色:

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

这也将自动着色刻度线。

自定义刻度和标签

您也可以单独自定义图中的对象。对于tick和ticklabels,有一个tick_params方法。例如,仅将颜色条的tick变为红色,

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

jbose2ul

jbose2ul4#

基于前面的答案,我添加了两行来设置colorbar的框颜色和colorbar的ticks颜色:

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")
dvtswwa3

dvtswwa35#

此外,您可以使用以下命令更改记号标签:

cax = plt.imshow(data)
cbar = plt.colorbar(orientation='horizontal', alpha=0.8, label ='my label',
                    fraction=0.075, pad=0.07, extend='max')
#get the ticks and transform it to list, if you want to add strings.
cbt = cbar.get_ticks().tolist() 
#edit the new list of ticks, for instance the firs element
cbt[0]='$no$ $data$'
# then, apply the changes on the actual colorbar
cbar.ax.set_xticklabels(cbt)

相关问题