You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改Python fitter包的绘图样式

如何修改fitter库输出绘图的外观

需求说明

需要调整fitter输出图的以下样式:

  • 直方图的填充颜色
  • 拟合线条的颜色
  • 给直方图的bins添加边缘线

此前仅能修改网格和边框样式,但无法调整直方图与拟合线条的属性。

解决方案

方法1:调用summary时直接传递样式参数

fitter的summary()方法支持通过hist_kwargs传递直方图样式参数,通过plot_kwargs传递拟合线条样式参数,无需额外遍历元素:

import fitter as ft
import matplotlib.pyplot as mp
from numpy.random import lognormal

data = lognormal(size=1000)

d = ['expon','weibull_min','pareto','gengamma','lognorm']
f = ft.Fitter(data, distributions=d, bins=50)
f.fit()

# 直接在summary中指定直方图和线条样式
ax = f.summary(
    lw=1.3,
    # 直方图样式:填充色、边缘色、边缘线宽
    hist_kwargs={'facecolor': '#87CEEB', 'edgecolor': '#333333', 'linewidth': 0.5},
    # 拟合线条颜色:按分布顺序指定颜色列表
    plot_kwargs={'color': ['#FF5733', '#33FF57', '#3357FF', '#FF33A6', '#FFC300']}
)

# 自定义网格和边框样式
ax.set_axisbelow(True)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.grid(color='#cccccccc', linestyle=':', linewidth=0.7, which='both')

mp.show()

方法2:绘图后获取元素修改样式

如果需要更灵活的调整,可以在调用summary()绘图后,获取轴对象上的直方图块和线条,逐个修改属性:

import fitter as ft
import matplotlib.pyplot as mp
from numpy.random import lognormal

data = lognormal(size=1000)

d = ['expon','weibull_min','pareto','gengamma','lognorm']
f = ft.Fitter(data, distributions=d, bins=50)
f.fit()

# 调用summary并获取轴对象
ax = f.summary(lw=1.3)

# 修改直方图样式
for patch in ax.patches:
    patch.set_facecolor('#87CEEB')    # 设置直方图填充色
    patch.set_edgecolor('#333333')    # 设置直方图边缘色
    patch.set_linewidth(0.5)          # 设置边缘线宽

# 修改拟合线条颜色
line_colors = ['#FF5733', '#33FF57', '#3357FF', '#FF33A6', '#FFC300']
for line, color in zip(ax.get_lines(), line_colors):
    line.set_color(color)

# 自定义网格和边框样式
ax.set_axisbelow(True)
ax.spines['right'].set_visible(False)
ax.spines['top'].set_visible(False)
ax.grid(color='#cccccccc', linestyle=':', linewidth=0.7, which='both')

mp.show()

说明

  • hist_kwargs支持所有matplotlib的plt.hist()关键字参数
  • plot_kwargs支持所有matplotlib的plt.plot()关键字参数
  • 方法1更简洁,方法2适合需要复杂自定义的场景

内容的提问来源于stack exchange,提问作者Erdem Şen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 21:27:16