Python学习笔记(十)

对应第十五章折线图部分

import matplotlib.pyplot as plt

squares= [1,4,9,16,25]
fig,ax = plt.subplots()
ax.plot(squares)

ax.set_title("平方数",fontsize=24)
ax.set_xlabel("值",fontsize=14)
ax.set_ylabel("平方",fontsize=14)

ax.tick_params(axis='both',labelsize=14)
plt.show()

如果代码中没有 import matplotlib 会有字体警告:RuntimeWarning: Glyph missing from current font. font.set_text(s, 0.0, flags=flags)

并且图片中原本的汉字将会变成方框

事实上,这是python的经典字体警告,需要在set_title之前将字体设置为楷体

matplotlib.rcParams['font.sans-serif'] = ['KaiTi']

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZjjNtx9e-1611572699537)(https://s3-us-west-2.amazonaws.com/secure.notion-static.com/fe354e6f-004b-4c0a-97c5-eaa3f8d73bab/squares.png)]

内置样式(如seaborn)中并不包含汉字字体,即使使用内置样式仍需先定义字体

D:\Program Files\Anaconda\lib\site-packages\matplotlib\backends\backend_agg.py:211: RuntimeWarning: Glyph 24179 missing from current font.
  font.set_text(s, 0.0, flags=flags)

在使用scatter时,参数c=’'的使用会引起warning

ax.scatter(x_value,y_value,c=(0,0.2,0),s=10)

‘c’ argument looks like a single numeric RGB or RGBA sequence, which should be avoided as value-mapping will have precedence in case its length matches with ‘x’ & ‘y’. Please use a 2-D array with a single row if you really want to specify the same RGB or RGBA value for all points.

初步判断为matplotlib的版本问题,高版本已经不会报warning

可将语句修改为:

ax.scatter(x_value,y_value,color=(0,0.2,0),s=10)

或者让日志只显示error级别的错误

from matplotlib.axes._axes import _log as matplotlib_axes_logger
matplotlib_axes_logger.setLevel('ERROR')

进一步:这个错误在使用颜色映射的时候不会出现

如果在颜色映射中使用color而非c则会报错

ax.scatter(x_value,y_value,color=y_value,cmap=plt.cm.Greens,s=10)

>>>ValueError: 'color' kwarg must be an mpl color spec or sequence of color specs.
For a sequence of values to be color-mapped, use the 'c' argument instead.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值