pandas 更改图表边框区域颜色

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23444413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:59:44  来源:igfitidea点击:

change a charts border area color

pythonmatplotlibpandas

提问by Uninvited Guest

Is it possible to set the area outside of the chart to the black color? I have the chart area set to black but the outside area has a grey color. Can i change this to black and maybe set the axis color to white if they are not visible?

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

I make a chart like this:

我做了一个这样的图表:

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()

采纳答案by Ffisegydd

The border you're referring to can be modified using the facecolorattribute. The easiest way to modify this with your code would be to use:

您所指的边框可以使用该facecolor属性进行修改。使用您的代码修改它的最简单方法是使用:

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

Alternatively you can set it using a keyword argument if you create the figure manually.

或者,如果您手动创建图形,您可以使用关键字参数设置它。

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()

Plot

阴谋

回答by jdhao

Another solution which is less flexible than @Ffisegydd's answer but easier is that you can use style 'dark_background' predefined in pyplot module to achieve a similar effect. The code is:

另一个比@Ffisegydd 的答案灵活但更简单的解决方案是,您可以使用 pyplot 模块中预定义的样式“dark_background”来实现类似的效果。代码是:

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

# use style 'dark_background'
plt.style.use('dark_background')
test = pd.DataFrame(np.random.randn(100,3))

chart = test.cumsum().plot()
#chart.set_axis_bgcolor('black')

plt.show()

The above code produces the following image.

上面的代码产生下图.

P.S.

聚苯乙烯

You can run plt.style.availableto print a list of available styles, and have fun with these styles.

您可以运行plt.style.available以打印可用样式列表,并享受这些样式的乐趣。

References

参考

  1. See herefor a detailed explanations on how to use styles or compose custom styles and more...

  2. Someone has made a webpagewhich shows the display effect of all predefined styles. Pretty awesome!

  1. 有关如何使用样式或编写自定义样式等的详细说明,请参见此处...

  2. 有人做了一个网页,展示了所有预定义样式的显示效果。非常棒!