微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python – 更改图表边框区域颜色

是否可以将图表外的区域设置为黑色?我有图表
区域设置为黑色,但外部区域为灰色.我可以将其更改为黑色,如果它们不可见,可能会将轴颜色设置为白色吗?

我做了一个这样的图表:

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

test = pd.DataFrame(np.random.randn(100,3))

chart = test.cumsum().plot()
chart.set_axis_bgcolor('black')
plt.show()

解决方法:

您可以使用facecolor属性修改您引用的边框.使用您的代码修改方法的最简单方法是使用:

plt.gcf().set_facecolor('white') # Or any color

或者,如果手动创建图形,则可以使用关键字参数进行设置.

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

test = pd.DataFrame(np.random.randn(100,3))

bkgd_color='black'
text_color='white'

fig = plt.figure(facecolor=bkgd_color)

ax = fig.add_subplot(1, 1, 1)

chart = test.cumsum().plot(ax=ax)
chart.set_axis_bgcolor(bkgd_color)

# Modify objects to set colour to text_color

# Set the spines to be white.
for spine in ax.spines:
    ax.spines[spine].set_color(text_color)

# Set the ticks to be white
for axis in ('x', 'y'):
    ax.tick_params(axis=axis, color=text_color)

# Set the tick labels to be white
for tl in ax.get_yticklabels():
    tl.set_color(text_color)
for tl in ax.get_xticklabels():
    tl.set_color(text_color)

leg = ax.legend(loc='best') # Get the legend object

# Modify the legend text to be white
for t in leg.get_texts():
    t.set_color(text_color)

# Modify the legend to be black
frame = leg.get_frame()
frame.set_facecolor(bkgd_color)

plt.show()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐