Python数据可视化
程序员文章站
2022-07-14 16:37:25
...
一、数据源
链接:https://pan.baidu.com/s/1oczoVEUIRHEyjEHsh3adPw
提取码:sgiu
复制这段内容后打开百度网盘手机App,操作更方便哦
二、打开数据
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data=pd.read_csv('data.csv',index_col='年份')
data.head()
三、绘制折线图
绘制人均GDP走势图:
x=data.index.values
y=data['人均GDP(元)'].values
from pylab import mpl
mpl.rcParams['font.sans-serif']=['FangSong']#指定默认字体
fig,ax=plt.subplots()
plt.plot(x,y,'r--*')
ax.set(title='人均GDP走势图',xlabel='年份',ylabel='人均GDP(元)')
plt.show()
绘制三个指标:
data.plot()
plt.show()
四、绘制柱形图
绘制人均GDP柱形图:
data['人均GDP(元)'].plot(kind='bar',color='skyblue')
plt.show()
绘制三个指标柱形图:
data.plot(kind='bar')
plt.show()
绘制堆积柱形图:
data.plot(kind='bar',stacked=True)
plt.show()
五、绘制散点图
绘制二维散点图:
df=np.random.randint(0,100,size=[40,40])
x,y=df[0],df[1]
ax=plt.subplot(111)
ax.scatter(x,y,c='g')
plt.show()
绘制三维散点图:
from mpl_toolkits.mplot3d import Axes3D
x,y,z=df[0],df[1],df[2]
ax=plt.subplot(111,projection='3d')
ax.scatter(x[:10],y[:10],z[:10],c='b')
ax.scatter(x[10:20],y[10:20],z[10:20],c='r')
ax.scatter(x[20:30],y[20:30],z[20:30],c='g')
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
plt.show()
六、绘制饼图
fig,ax=plt.subplots()
ax.pie(y[:5],labels=x[:5],explode=[0,0.05,0.1,0.15,0.2])
plt.show()
七、绘制词云图
jieba分词:
from wordcloud import WordCloud
import jieba
print(list(jieba.cut("我是一个中国人,来自上海!")))
输出:
链接:https://pan.baidu.com/s/1zMnXfpGn2lXFi3QhrUekKA
提取码:u4y9
复制这段内容后打开百度网盘手机App,操作更方便哦
with open('paragraph.txt',encoding='utf-8') as file:
text=file.read()
wordlist_jieba=jieba.cut(text)
wordlist='/'.join(wordlist_jieba)
wordcloud=WordCloud(font_path='C:/Windows/Fonts/迷你简少儿.ttf').generate(wordlist)#在C:\Windows\Fonts中找一种字体,否则会乱码
image=wordcloud.to_image()
image.show()
设置样式,绘制指定背景的词云图:
from PIL import Image
images=Image.open('heart.png')#插入背景图
maskImage=np.array(images)
wordcloud=WordCloud(font_path='C:/Windows/Fonts/迷你简少儿.ttf',
background_color='white',
width=600,height=300,
mask=maskImage).generate(wordlist)
image=wordcloud.to_image()
image.show()
上一篇: 学习资源总结
下一篇: hadoop自定义排序,分组排序