美文网首页
可视化图表

可视化图表

作者: haokeed | 来源:发表于2019-05-09 15:49 被阅读0次

基本用法

import matplotlib.pyplot as plt
# 必须先安装matplotlib
# pip install matplotlib

#准备绘图数据
import numpy as np
x=np.arange(0,1,0.05) # 0-1的数据 步长为0.05
print(x)
#y=sin(2*pi*x)
y=np.sin(2*np.pi*x)
print(y)

#plt.plot
plt.plot(x,y)
plt.show()

plt.plot(x,y,'b--*',label='sin') # 加载数据 设置颜色为blue 设置线条为虚线 设置点位* 设置标注文字为sin
plt.title('My First Plot') # 图表标题
plt.xlabel('x label') # x轴标签
plt.ylabel('y label') # y轴标签
plt.legend(loc='best') # 标注位置 best表示系统安排最佳
plt.show()

# figure和subplot
# 绘制多个图表
fig=plt.figure() #图表集合
ax1=fig.add_subplot(221) # 设置一个2x2的图标,将改图表放到第1个
ax2=fig.add_subplot(222) # 设置一个2x2的图标,将改图表放到第2个
ax3=fig.add_subplot(223) # 设置一个2x2的图标,将改图表放到第3个
ax4=fig.add_subplot(224) # 设置一个2x2的图标,将改图表放到第4个
plt.show()

fig=plt.figure()
ax1=fig.add_subplot(221)
ax2=fig.add_subplot(222)
ax3=fig.add_subplot(223)
ax2.plot(x,y)
fig.show()

# 设置颜色 线型 标记
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,'r--*') # 设置颜色为read 线为虚线 点用*表示
plt.show()

# 设置颜色 线型 标记
fig=plt.figure()
ax=fig.add_subplot(111)
ax.plot(x,y,color='b',linestyle='--',marker='o') # 设置颜色为blue 线为虚线 点用圆点表示
plt.show()

# 设置图标格子类型
fig,ax=plt.subplots(2,2) #设置2X2的格子 每个格子都一个图表 图表对象赋值给fig 图标集合赋值给ax ax可以通过数组方式管理
ax[0,1].plot(x,y) # 将第一行 第二列的图表附上数据
plt.show()

# 标题 标签 图例
fig,ax=plt.subplots() #默认就是1x1的
ax.plot(x,y,'g--o',label='sin') # 设置数据 线条颜色 线条样式 点类型 标注文字
ax.set(title='My first Plot',xlabel='x',ylabel='y') # 设置标题 x轴文字 y轴文字
ax.legend(loc='best') # 设置标注位置
ax.grid() # 设置网格
fig.show()

#y2=cos(2*pi*x)
y2=np.cos(2*np.pi*x)
print(y2)

# 一个图表中多个个线数据
fig,ax=plt.subplots()
ax.plot(x,y,'b--*',label='sin') # 添加线条1
ax.plot(x,y2,'r--o',label='cos') # 添加线条2
ax.legend(loc='upper center') # 设置标注位置 best最佳 upper 上方 lower下方 center 中心 left 左边 right 右边
ax.set(title='sin&cos')
fig.show()

#将图表保存到本地
fig.savefig('myfig.png')
image.png
image.png
image.png
image.png
image.png
image.png

常用图表绘制

# 数据导入
import pandas as pd

# 
df=pd.read_csv('./data/data.csv')
df.head()


df=pd.read_csv('./data/data.csv',index_col='年份')
df.head()

len(df)

df['人均GDP(元)'].head()

y=df['人均GDP(元)'].values
print(y)
x=df.index.values
print(x)

#指定默认字体
from pylab import mpl

mpl.rcParams['font.sans-serif']=['FangSong']


# 折线图
fig,ax=plt.subplots()
ax.plot(x,y,'r')
ax.set(title='人均GDP走势图',xlabel='年份',ylabel='人均GDP')
plt.show()

# 柱形图
fig,ax=plt.subplots()
ax.bar(x,y)
plt.show()

fig,ax=plt.subplots()
ax.bar(x,y,0.5,color='skyblue') # 透明度未0.5 柱状颜色为skyblue
ax.set(title='人均GDP走势图',xlabel='年份',ylabel='人均GDP')
plt.show()

# 可以不用set 采用cet_*的方式单独设置参数
fig,ax=plt.subplots()
ax.bar(x,y,0.5,color='skyblue')
ax.set_title('人均GDP走势图') 
ax.set_xlabel('年份')
ax.set_ylabel('人均GDP')
plt.show()


#水平柱形图——条形图
fig,ax=plt.subplots()
ax.barh(x,y,0.5,color='skyblue')
ax.set_title('人均GDP走势图') 
ax.set_xlabel('年份')
ax.set_ylabel('人均GDP')
plt.show()


# 饼图
fig,ax=plt.subplots()
ax.pie(y,labels=x)
plt.show()

fig,ax=plt.subplots()
ax.pie(y[:5],labels=x[:5])
fig.show()

fig,ax=plt.subplots()
ax.pie(y[:5],labels=x[:5],explode=[0,0.05,0.1,0.15,0.2]) # 设置各个快之间的距离


# 散点图
scatter_df=pd.read_csv('./data/BankData.csv',index_col='分行编号')
scatter_df.head()

len(scatter_df)

x=scatter_df['各项贷款余额']
x.head()

y=scatter_df['不良贷款(亿元)']
y.head()

fig,ax=plt.subplots()
ax.scatter(x,y)
plt.show()

fig,ax=plt.subplots()
ax.scatter(x,y,alpha=0.5) # 设置透明度
ax.set_title('散点图')
ax.set_xlabel('各项贷款余额',fontsize=12) # 设置x轴坐标名称  和字体大小
ax.set_ylabel('不良贷款(亿元)',fontsize=12)
ax.grid()
plt.show()
image.png
image.png
image.png
image.png
image.png
image.png
image.png
image.png

Pandas中的绘图函数

df.head()

y=df['人均GDP(元)'].values
print(y)
x=df.index.values
print(x)

fig,ax=plt.subplots()
ax.plot(x,y,'r')
ax.set(title='人均GDP走势图',xlabel='年份',ylabel='人均GDP')
plt.show()

#折线图
df['人均GDP(元)'].plot(color='r')

# 柱状图
df['人均GDP(元)'].plot(kind='bar',color='skyblue',title='折线图')

# 水平柱状图
df['人均GDP(元)'].plot(kind='barh',color='skyblue')

# 饼图
df['人均GDP(元)'].plot(kind='pie')

# 面积图
df['人均GDP(元)'].plot(kind='area')

df.head()

df.plot()

df.plot(kind='bar')

df.plot(kind='bar',stacked=True)

# 直方图
from pandas import Series

hist_data=Series([2,3,4,5,2,3,3,4,6,5])
hist_data.plot(kind='hist')

hist_data=Series([2,3,4,5,2,3,3,4,6,5])
mybins=[1,3,5,7]
hist_data.plot(kind='hist',bins=mybins)
image.png
image.png
image.png
image.png
image.png
image.png
image.png

相关文章

网友评论

      本文标题:可视化图表

      本文链接:https://www.haomeiwen.com/subject/dunioqtx.html