数据可视化–对DataFrame的数据进行画图
dataframe有自带的函数plot进行画图,需要引入matplotlib的pyplot库
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
df = pd.DataFrame(3 * np.random.rand(4),index=[u'衣服',u'裤子',u'裙子',u'袜子'])
df.plot.pie(subplots=True)
#随机化数组
可是简单的尝试后发现,标签输出为中文却有乱码,如图所示
如何才能使图表支持中文标签呢?
需要设置plt的字体,加入下面一行就行
plt.rcParams['font.sans-serif']=['SimHei']
就可以正确地输出中文标签辣
这里自带的颜色可以说是非常丑了,如何美化matplotlib默认的颜色呢?
有两种方法
一、自己设定color集
这里我选择了几种特殊的,低饱和度的比较养眼的色彩,使用参数colors=设定应用后,再输出,图表如下,是不是好看多了
colors=["lightskyblue","steelblue","sandybrown","darkgrey","coral","pink","rosybrown","lightblue","plum","lightsalmon","salmon","mediumaquamarine","mediumseagreen"]
df.plot.pie(subplots=True,colors=colors)
二、使用style进行设定
可以使用plt.style.use()对输出的style进行设定。style的列表可以在如下网址中查询
https://matplotlib.org/3.1.1/gallery/style_sheets/style_sheets_reference.html
plt.style.use('ggplot')
使用后的输出,如图