在matplotlib中更改坐标轴的颜色,刻度和标签

我想改变轴的颜色,以及我使用matplotlib和PyQt绘图的刻度和值标签。

有任何想法吗?

作为一个简单的例子(使用比可能重复的问题更清洁的方法):

import matplotlib.pyplot as plt fig = plt.figure() ax = fig.add_subplot(111) ax.plot(range(10)) ax.set_xlabel('X-axis') ax.set_ylabel('Y-axis') ax.spines['bottom'].set_color('red') ax.spines['top'].set_color('red') ax.xaxis.label.set_color('red') ax.tick_params(axis='x', colors='red') plt.show() 

替代文字

如果您有多个要修改的graphics或子图,则可以使用matplotlib上下文pipe理器来更改颜色,而不是逐个更改颜色。 上下文pipe理器允许您临时更改rc参数,仅用于紧随其后的缩进代码,但不会影响全局rc参数。

这段代码产生了两个数字,第一个是修改了轴的颜色,ticks和ticklabels,第二个是默认的rc参数。

 import matplotlib.pyplot as plt with plt.rc_context({'axes.edgecolor':'orange', 'xtick.color':'red', 'ytick.color':'green', 'figure.facecolor':'white'}): # Temporary rc parameters in effect fig, (ax1, ax2) = plt.subplots(1,2) ax1.plot(range(10)) ax2.plot(range(10)) # Back to default rc parameters fig, ax = plt.subplots() ax.plot(range(10)) 

在这里输入图像说明

在这里输入图像说明

您可以键入plt.rcParams来查看所有可用的rc参数,并使用list comprehensionsearch关键字:

 # Search for all parameters containing the word 'color' [(param, value) for param, value in plt.rcParams.items() if 'color' in param]