在matplotlib中优雅地更改绘图框的颜色

s71maibg  于 2023-03-03  发布在  其他
关注(0)|答案(4)|浏览(328)

这是this帖子的一个后续问题,在那里讨论了轴,刻度和标签的着色。我希望可以为此打开一个新的,扩展的问题。
更改轴为[ax 1,ax 2]的双绘图(通过add_subplot)周围的完整框架(刻度和轴)的颜色会导致大量代码。以下代码段更改了上绘图 * 的框架 * 的颜色:

ax1.spines['bottom'].set_color('green')
ax1.spines['top'].set_color('green')
ax1.spines['left'].set_color('green')
ax1.spines['right'].set_color('green')
for t in ax1.xaxis.get_ticklines(): t.set_color('green')
for t in ax1.yaxis.get_ticklines(): t.set_color('green')
for t in ax2.xaxis.get_ticklines(): t.set_color('green')
for t in ax2.yaxis.get_ticklines(): t.set_color('green')

因此,要更改两个图(每个图有两个y轴)的边框颜色,我需要16(!)行代码...

到目前为止我发现的其他方法:

  • matplotlib.rc:讨论了here;全局变化,而不是局部变化。我想有一些其他不同颜色的图。请不要讨论图中颜色太多...:-)
matplotlib.rc('axes',edgecolor='green')
  • 挖出轴的刺,然后改变它:也讨论了here;我觉得不是很优雅。
for child in ax.get_children():
    if isinstance(child, matplotlib.spines.Spine):
        child.set_color('#dddddd')

有没有一种优雅的方式来压缩上面的块,更“Python”的东西?
我在ubuntu下使用python 2.6.5和matplotlib0.99.1.1。

eoxn13cs

eoxn13cs1#

假设你使用的是matplotlib的最新版本(〉= 1.0),也许可以尝试如下操作:

import matplotlib.pyplot as plt

# Make the plot...
fig, axes = plt.subplots(nrows=2)
axes[0].plot(range(10), 'r-')
axes[1].plot(range(10), 'bo-')

# Set the borders to a given color...
for ax in axes:
    ax.tick_params(color='green', labelcolor='green')
    for spine in ax.spines.values():
        spine.set_edgecolor('green')

plt.show()

nfs0ujit

nfs0ujit2#

重构上面的代码:

import matplotlib.pyplot as plt

for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']):
    plt.setp(ax.spines.values(), color=color)
    plt.setp([ax.get_xticklines(), ax.get_yticklines()], color=color)
62o28rlo

62o28rlo3#

也许回答我自己的问题有点粗糙,但我想分享一下我目前所发现的。这个版本可以用两种不同的颜色给两个坐标轴为[ax1, ax2][ax3, ax4]的子图着色。它比我在上面的问题中提到的16行要短 * 很多a。它的灵感来自Joe Kington的回答,在twinx中删除了勾选标记颜色。

import matplotlib.pyplot as plt
import numpy as np

# Generate some data
num = 200
x = np.linspace(501, 1200, num)
yellow_data, green_data , blue_data= np.random.random((3,num))
green_data += np.linspace(0, 3, yellow_data.size)/2
blue_data += np.linspace(0, 3, yellow_data.size)/2

fig = plt.figure()
plt.subplot(211) # Upper Plot
ax1 = fig.add_subplot(211)
ax1.fill_between(x, 0, yellow_data, color='yellow')
ax2 = ax1.twinx()
ax2.plot(x, green_data, 'green')
plt.setp(plt.gca(), xticklabels=[])
plt.subplot(212) # Lower Plot
ax3 = fig.add_subplot(212)
ax3.fill_between(x, 0, yellow_data, color='yellow')
ax4 = ax3.twinx()
ax4.plot(x, blue_data, 'blue')

# Start coloring
for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']):
    for ticks in ax.xaxis.get_ticklines() + ax.yaxis.get_ticklines():
        ticks.set_color(color)
    for pos in ['top', 'bottom', 'right', 'left']:
        ax.spines[pos].set_edgecolor(color)
# End coloring

plt.show()

我把它标记为接受,因为这是我目前能找到的最简洁的解决方案,不过,我还是愿意用其他可能更优雅的方法来解决它。

7tofc5zh

7tofc5zh4#

在Matplotlib的最新版本中,这变得更简单了。现在,要给刻度、刻度标签和书脊着色,OP采用的是以下样式:

for ax, color in zip([ax1, ax2, ax3, ax4], ['green', 'green', 'blue', 'blue']):
    ax.tick_params(color=color, labelcolor=color)
    ax.spines[:].set_color(color)

相关问题