import matplotlib.pyplot as plt
import numpy as np
x=np.arange(0,1,0.05)
print(x)
[0. 0.05 0.1 0.15 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65
0.7 0.75 0.8 0.85 0.9 0.95]
#y=sin(2*pi*x)
y=np.sin(2*np.pi*x)
print(y)
[ 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')
plt.title('My First Plot')
plt.xlabel('x label')
plt.ylabel('y label')
plt.legend(loc='best')
plt.show()
fig,ax=plt.subplots(2,2)
ax[0,1].plot(x,y)
plt.show()
#y2=np.cos(2*pi*x)
y2=np.cos(2*np.pi*x)
print(y2)
[ 1.00000000e+00 9.51056516e-01 8.09016994e-01 5.87785252e-01
3.09016994e-01 6.12323400e-17 -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.83697020e-16
3.09016994e-01 5.87785252e-01 8.09016994e-01 9.51056516e-01]
fig,ax=plt.subplots()
ax.plot(x,y,'g--*',label='sin')
ax.plot(x,y2,'r--o',label='cos')
ax.set(title='My First Plot')
legend=ax.legend(loc='upper center')
plt.show()
#保存
fig.savefig('myfig.png')
#读取数据
import pandas as pd
df=pd.read_csv('../data/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
from pylab import mpl
mpl.rcParams['font.sans-serif']=['FangSong'] #指定默认字体
fig,ax=plt.subplots()
ax.plot(x,y,'r--*')
ax.set(title='人均GDP走势图',xlabel='年份')
plt.show()
fig,ax=plt.subplots()
ax.pie(y[:5],labels=x[:5])
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()
#绘制词云图
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()
wordcloud=WordCloud(font_path='C:/Windows/Fonts/simfang.ttf',
background_color='black',width=600,height=300,max_words=50).generate(words)
image=wordcloud.to_image()
image.show()
#绘制指定形状的词云图
from PIL import Image
images=Image.open('heart.png')
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数据可视化
最新推荐文章于 2023-06-05 08:15:00 发布