Matplotlib: 覆盖“ggplot”默认样式属性

6

我正在使用matplotlib的ggplot风格进行绘图,希望仅覆盖特定的标准参数,如xticklabels的颜色、网格背景颜色和线宽。

import numpy as np
import pandas as pd
import matplotlib

# changing matplotlib the default style
matplotlib.style.use('ggplot')

# dataframe plot
df = pd.DataFrame(np.random.randn(36, 3))
df.plot()

返回结果: enter image description here

我知道可以像这样为轴对象设置单个属性:

ax.set_axis_bgcolor('red')

但是我该如何覆盖默认属性(例如标签颜色、背景颜色和线宽),以便在所有图中使用?

非常感谢!

1个回答

12

您可以使用rcParams在全局范围内设置参数,例如:

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

# changing matplotlib the default style
matplotlib.style.use('ggplot')

plt.rcParams['lines.linewidth']=3
plt.rcParams['axes.facecolor']='b'
plt.rcParams['xtick.color']='r'

# dataframe plot
df = pd.DataFrame(np.random.randn(36, 3))
df.plot()

plt.show()

enter image description here


非常好,谢谢!有没有办法全局更改轴标签颜色? - Cord Kaldemeyer
在我回答中的链接中显示了你可以更改的所有内容。对于轴标签,可以使用plt.rcParams['axes.labelcolor']='r'。不确定是否可以分别更改x和y。 - tmdavison
太好了,你救了我的一天。非常感谢!! - Cord Kaldemeyer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接