# 创建数据,可视化
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,1,0.05)
# 正弦函数y = sin(2*np.pi*x)
# 2×np.pi就相当于2π
y = np.sin(2*np.pi*x)
y
array([ 0.00000000e+00, 3.09016994e-01, 5.87785252e-01, 8.09016994e-01, 9.51056516e-01, 1.00000000e+00, 9.51056516e-01, 8.09016994e-01, 5.87785252e-01, 3.09016994e-01, 1.22464680e-16, -3.09016994e-01, -5.87785252e-01, -8.09016994e-01, -9.51056516e-01, -1.00000000e+00, -9.51056516e-01, -8.09016994e-01, -5.87785252e-01, -3.09016994e-01])
plt.plot(x,y,'b--*',label='sin') # 这个b表示线的颜色,--表示线,*表示数据点
plt.rcParams['font.sans-serif']=['SimHei'] #解决乱码问题
plt.rcParams['axes.unicode_minus']=False #解决有些符号可以显示出来
plt.title('My plot')
plt.xlabel('x')
plt.legend(loc='best') # 图例显示的位置
plt.show()
fig,ax=plt.subplots(2,2) # 创建一个2*2 的绘图区域
ax[0,1].plot(x,y)
plt.show()
fig,ax=plt.subplots()
ax.plot(x,y,'g--*',label = 'sin')
ax.plot(x,y2,'r--*',label = 'cos')
ax.set(title = 'Project')
ax.legend(loc = 'best')
plt.show()
# 保存到本地
fig.savefig('myfig.png')
2、从外部读取数据可视化展示
import pandas as pd
import os
os.chdir(r'E:\亚信\新建文件夹\可视化分析\Excel1')
df = pd.read_csv('data.csv',index_col = '年份')
df.head()
人均GDP(元) | 啤酒产量(万千升) | 居民消费价格指数(上面=100) | |
---|---|---|---|
年份 | |||
2000 | 7857.7 | 2231.3 | 100.4 |
2001 | 8621.7 | 2288.9 | 100.7 |
2002 | 9398.1 | 2402.7 | 99.2 |
2003 | 10542.0 | 2540.5 | 101.2 |
2004 | 12335.6 | 2948.6 | 103.9 |
x = df.index.values
y = df['人均GDP(元)'].values
fig,ax = plt.subplots()
plt.rcParams['font.sans-serif']=['SimHei'] #解决乱码问题
plt.rcParams['axes.unicode_minus']=False #解决有些符号可以显示出来
ax.plot(x,y,'r--*')
ax.set(title = '人均GDP走势图')
plt.show()
# 绘制饼图
fig,ax = plt.subplots()
ax.pie(y[:5],labels=x[:5],explode =[0,0.05,0.1,0.15,0.2] )# 设置x与y的取值范围
plt.show()
3、绘制词云图
# 读取
with open('classify_model1.csv',encoding='utf-8') as file:
words = file.read()
print(words)
from wordcloud import WordCloud
wordcloud = WordCloud(font_path = 'C:/Windows/Fonts/simfang.ttf').generate(words)
image = wordcloud.to_image()
image.show()
# 绘制指定形状的词云图
from PIL import Image
images =Image.open('heart.png')
images
maskImages=np.array(images)
wordcloud = WordCloud(font_path = 'C:/Windows/Fonts/simfang.ttf',
background_color = 'black',width=600,height=300,
max_words=50,mask=maskImages).generate(words)
image = wordcloud.to_image()
image.show()
利用Python进行可视化分析,今天就到这了😁