解决警告:UserWarning: FixedFormatter should only be used together with FixedLocator(图文并茂版!!!)

报错信息

       •当我们在绘制边缘直方图时,使用常规方法,将散点图的x轴刻度转换成浮点数时会出现下述警告!!!

UserWarning: FixedFormatter should only be used together with FixedLocator
  ax_main.set_xticklabels(xlabels)

在这里插入图片描述

问题代码

xlabels = ax_main.get_xticks().tolist() # 将刻度值转换成浮点数
ax_main.set_xticklabels(xlabels) # 设置刻度值为浮点数
plt.show()

       •当你使用上述代码将刻度值转换成浮点数时,就会出现如题目一样的警告,但是展示的散点图像的x轴刻度已经成功转换成浮点数如下图所示!!
在这里插入图片描述

问题分析

       •问题描述:这是一个用户警告:即为我们操作不规范导致的警告,它告诉我们FixedFormatter(刻度形式) 只能与 FixedLocator定位器 一起使用,而不能使用其他方法改变刻度形式!!!

解决问题

       •在上面我们分析了导致警告的原因,我们应该使用FixedLocator定位器来改变FixedFormatter(刻度形式),而不是直接转换刻度格式,导致警告!!!
       •首先导入matplotlib库中的ticker模块
代码如下:

import matplotlib.ticker as mticker

label_format = '{:,.1f}'  # 创建浮点数格式 .1f一位小数
xlabels = ax_main.get_xticks().tolist()
ax_main.xaxis.set_major_locator(mticker.FixedLocator(xlabels))  # 定位到散点图的x轴
ax_main.set_xticklabels([label_format.format(x) for x in xlabels])  # 使用列表推导式循环将刻度转换成浮点数
plt.show()

图像显示:
在这里插入图片描述

       •绘制上述图像的完整代码为:

import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import pandas as pd

# 获取数据
df = pd.read_csv(r'D:\9\mpg_ggplot2.csv')

# 创建画布并将画布分割成格子
fig = plt.figure(figsize=(16, 10), dpi=80, facecolor='white')
grid = plt.GridSpec(4, 4, hspace=0.5, wspace=0.2)

# 添加子图
ax_main = fig.add_subplot(grid[:-1, :-1])
ax_right = fig.add_subplot(grid[:-1, -1], xticklabels=[], yticklabels=[])
ax_bottom = fig.add_subplot(grid[-1, :-1], xticklabels=[], yticklabels=[])

# 在中心绘制气泡图
ax_main.scatter('displ', 'hwy'
                , s=df.cty * 4
                , data=df
                , c=df.manufacturer.astype('category').cat.codes
                , cmap='tab10'
                , edgecolors='gray'
                , linewidth=.5
                , alpha=.9)
# 绘制底部直方图
ax_bottom.hist(df.displ, 40, histtype='stepfilled', orientation='vertical', color='deeppink')
ax_bottom.invert_yaxis()  # 让y轴反向

# 绘制右边直方图
ax_right.hist(df.hwy, 40, histtype='stepfilled', orientation='horizontal', color='deeppink')

# 装饰图像
plt.rcParams['font.sans-serif'] = ['Simhei']
ax_main.set(title='边缘直方图 \n 发动机排量 vs 公路里程/加仑'
            , xlabel='发动机排量(L)'
            , ylabel='公路里程/加仑')
ax_main.title.set_fontsize = (20)

for item in ([ax_main.xaxis.label, ax_main.yaxis.label] + ax_main.get_xticklabels() + ax_main.get_yticklabels()):
    item.set_fontsize(14)

for item in [ax_bottom, ax_right]:
    item.set_xticks([])
    item.set_yticks([])

label_format = '{:,.1f}'  # 创建浮点数格式 .1f一位小数
xlabels = ax_main.get_xticks().tolist()
ax_main.xaxis.set_major_locator(mticker.FixedLocator(xlabels))  # 定位到散点图的x轴
ax_main.set_xticklabels([label_format.format(x) for x in xlabels])  # 使用列表推导式循环将刻度转换成浮点数
plt.show()
  • 20
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 10
    评论
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值