前言:python数据挖掘pandas+matplotlib+seaborn数据可视化(画图)的基础知识,简单总结,主要是为了方便自己写代码的时候查看,发现有用的方法,随时补充,欢迎指正
数据挖掘专栏
一、数据可视化介绍
数据可视化主要旨在借助于图形化手段,清晰有效地传达与沟通信息,美学形式与功能需要齐头并进,通过直观地传达关键的方面与特征,从而实现对于相当稀疏而又复杂的数据集的深入洞察。
所以这篇文章叫《利用pandas+matplotlib+seaborn画图》比较好
二、pandas
1、饼图
series.plot.pie()
In[38]: df = pd.DataFrame({'aaa':[1,2,3,4]})
In[39]: df['aaa'].plot.pie(autopct='%.2f',title='饼图');
2、条形图
df.plot.bar()
In[55]: df = pd.DataFrame(np.random.randn(4,6),index=['a','b','c','d'],columns=['A','B','C','D','E','F'])
In[56]: df.plot.bar(title='条形图')
3、折线图
df.plot.line()
In[60]: df = pd.DataFrame(np.random.randn(4,6),index=['a','b','c','d'],columns=['A','B','C','D','E','F'])
In[61]: df.plot.line(title='折线图')
4、散点图
df.plot.scatter()
In[67]: df = pd.DataFrame(np.random.randn(100,2),columns=['A','B'])
In[68]: df.plot.scatter(x='A',y='B',title='散点图')
三、seaborn
1、核密度直方图
sns.distplot()
In[76]: df = pd.DataFrame(np.random.randn(100,2),columns=['A','B'])
In[77]: sns.distplot(df['A']).set_title('核密度直方图')
2、热力图
sns.heatmap()
In[86]: df = pd.DataFrame(np.random.randn(10,10))
In[87]: sns.heatmap(df,cmap='rainbow').set_title('热力图')
四、matplotlib.patches生成图形
以足球场为例,patches包
def football_filed():
"""
@return:足球场图
"""
fig, ax = plt.subplots()
plt.axis([0, 100, 0, 100])
ax.invert_yaxis() # 反转y轴
rect1 = patches.Rectangle((0, 41), 5.5, 18, color='0', fill=False)
ax.add_patch(rect1)
rect2 = patches.Rectangle((0, 30), 16.5, 40, color='0', fill=False)
ax.add_patch(rect2)
rect3 = patches.Rectangle((94.5, 41), 5.5, 18, color='0', fill=False)
ax.add_patch(rect3)
rect4 = patches.Rectangle((83.5, 30), 16.5, 40, color='0', fill=False)
ax.add_patch(rect4)
c1 = patches.Circle((0, 0), 2, fill=False)
ax.add_patch(c1)
c2 = patches.Circle((0, 100), 2, fill=False)
ax.add_patch(c2)
c3 = patches.Circle((100, 0), 2, fill=False)
ax.add_patch(c3)
c4 = patches.Circle((100, 100), 2, fill=False)
ax.add_patch(c4)
c5 = patches.Ellipse((50, 50), 18.3, 25, fill=False)
ax.add_patch(c5)
plt.plot([50, 50], [0, 100], color="0")
return fig, ax
五、词云图
# -*- coding:utf-8 -*-
"""
@author: 1
@file: word_cloud.py
@time: 2020/3/21 21:49
"""
# word_cloud
from wordcloud import WordCloud, STOPWORDS
import pandas as pd
df = pd.read_csv('df1_new.csv', index_col=0)
text = ("".join(i for i in df['review']))
stopwords = set(STOPWORDS)
word_cloud = WordCloud(stopwords=stopwords, background_color='white').generate(text)
image = word_cloud.to_image()
image.show()
word_cloud.to_file(r'C:\Users\1\WorkSpace\python\word_cloud.png')