如何在Python Pandas中更改`df.plot()`的背景颜色?

huangapple go评论49阅读模式
英文:

How to change the background color of df.plot() in Python Pandas?

问题

我想要指定使用Pandas/Python中的df.plot()创建的绘图周围的颜色。

在下面的代码中使用.set_facecolor只会改变坐标轴内部的颜色(参见图像),我想要改变外部的颜色。

将最后一行替换为以下两行会产生相同的图形。

ax = df.plot('PC1','PC2','scatter')
ax.set_facecolor('green')

setfacecolor示例

英文:

I want to specify the color of the area surrounding a plot created using the df.plot() in Pandas/Python.

Using .set_facecolor as in the code below only changes the area inside the axes (see image), I want to change the color outside too.

import pandas as pd
import numpy as np

df = pd.DataFrame(components, columns=['PC1','PC2']
df.plot('PC1','PC2','scatter').set_facecolor('green')

Replacing the last line with these two lines produces the same graph.

ax = df.plot('PC1','PC2','scatter')
ax.set_facecolor('green')

如何在Python Pandas中更改`df.plot()`的背景颜色?
setfacecolor example

答案1

得分: 0

IIUC,您可以使用fig.set_facecolor

fig, ax = plt.subplots()
df.plot('PC1', 'PC2', 'scatter', ax=ax).set_facecolor('green')
fig.set_facecolor('green')
plt.show()

输出:

如何在Python Pandas中更改`df.plot()`的背景颜色?

英文:

IIUC, you can use fig.set_facecolor:

fig, ax = plt.subplots()
df.plot('PC1','PC2','scatter', ax=ax).set_facecolor('green')
fig.set_facecolor('green')
plt.show()

Output:

如何在Python Pandas中更改`df.plot()`的背景颜色?

huangapple
  • 本文由 发表于 2023年2月7日 00:54:44
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364303.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定