数据可视化利器pyechart和matplotlib比较
python中用作数据可视化的工具有多种,其中matplotlib最为基础。故在工具选择上,图形美观之外,操作方便即上乘。
本文着重说明常见图表用基础版matplotlib和改良版pyecharts作图间的差异
一、maplotlib
基本用法如下:
import numpy as np import pandas as pd from pandas import Series, DataFrame import matplotlib.pyplot as plt import matplotlib.finance as mpf %matplotlib inline fig = plt.figure(figsize=(10,8)) #建立一个大小为10*8的画板 ax1 = fig.add_subplot(321) #在画板上添加3*3个画布,位置是第1个 ax2 = fig.add_subplot(3,2,2) ax3 = fig.add_subplot(3,2,3) ax4 = fig.add_subplot(3,2,4) ax5 = fig.add_subplot(3,2,5) ax6 = fig.add_subplot(3,2,6) data1 = np.random.randn(20) x = np.arange(20) ax1.plot(data1) #作折线图 ax2.scatter(data1,data1,color='r') #作散点图 ax3.hist(data1,bins=10,alpha=0.3) #作直方图 ax4.bar(x,data1) #作柱形图 ax5.pie(np.random.randint(1,15,5),explode=[0,0,0.2,0,0]) #作饼形图 ax6.plot(x,data1,color='green') #组合图 ax6.bar(x,data1,color='k') fig,axes = plt.subplots(3,3,figsize=(20,16))
data2 = DataFrame(np.random.randn(10,5),columns = ['A','B','C','D','E'],index = np.arange(0,100,10))
data2.plot(kind='line',ax=axes[0][0]) #作折线图
data2.plot(kind = 'scatter',x = data2.index[0] ,y = data2.index[0],ax=axes[0][1]) #作散点图
data2.plot(kind='hist',ax=axes[0][2],legend=False) #作直方图
data2.plot(kind='bar',ax=axes[1][0]) #作柱形图
data2.plot(kind='area',ax=axes[1][1],stacked = False) #面积图
train_data = np.array(data2)
mpf.candlestick_ohlc(axes[1][2],train_data.tolist(),width=1.5,colorup='r',colordown='g') #K线图
axim = axes[2][0].imshow(data2.values,interpolation='nearest') #热力图
plt.colorbar(axim)
plt.legend(loc = 'best')
plt.show()
可能遇到的问题:
1.axes[ ][ ]前面的代表行,后面代表列
2.散点图表示因变量随自变量而变化的大致趋势,x,y大小写有区分
3.面积图若不添加stacked = False会报错,提示每列必须得全正或全负
4.K线图至少得5列数据
更多操作:
plt.xlabel('横坐标名称') plt.title('标题') plt.xlim([0,20]) #添加x轴范围 ax.set_xyick([0,250,500,750,1000]) #设置x轴标签 ax.text(x,y,'text',family = '',fontsize = '') #添加文本 plt.setp(ax.get_xticklables(),viseble = False) #隐藏x轴标签,多图使用 plt.subplot_adjust(left = None,right = None,top = None,wspace = None,hspace = None) #调整子图间距 plt.axhline(y = 0,linewidth = 1,color = 'green') #添加分割线 plt.grid(True) #添加网格 plt.savefig('filepath') #保存图片
二、pyecharts
基本用法如下:
import pandas as pd import numpy as np from pyecharts import Page #pandas类型的数据处理 index = pd.date_range('2/1/2017',periods = 6,freq = 'M') df1 = pd.DataFrame(np.random.randn(6),index = index) df2 = pd.DataFrame(np.random.randn(6),index = index) df1_value = [i[0] for i in df1.values] df2_value = [i[0] for i in df2.values] #print type(df1_value) 列表 #pandas类型转list类型 data = ts.get_hist_data('300348',start='2017-01-01') new_data = data.ix[:,['open','close','high','low']] train_data = np.array(new_data) train_index = np.array(data.index) #元组列表 d = [(11,19),(12,21),(13,32),(10,20),(10,20),(10,33)] dat = dict(d) d1 = dat.keys() d2 = dat.values() #print type(d1) 列表 #列表数据 attr = ['衬衫','羊毛衫','雪纺衫','裤子','高跟鞋','袜子'] v1 = [11,12,13,10,10,10] v2 = [19,21,32,20,20,33] from pyecharts import Bar page = Page() #实例化,同一网页按顺序展示多图 bar = Bar('bar示例图','ryana') bar.add('test',index,df1_value,mark_point = ['average']) bar.add('test',attr,v1,mark_line = ['max']) page.add(bar) from pyecharts import Line line = Line('折线图示例') #line.add('test',attr,v1,mark_point = 'average') line.add('test',attr,v2,is_fill = True,area_opacity = 0.3) #面积图 page.add(line) from pyecharts import Pie pie = Pie('饼图示例') #pie.add('test',attr,v1,is_label_show = True) pie.add('test',attr,v2,radius = [40,75],is_label_show = True) #饼环图 page.add(pie) """ #组合图line+pie from pyecharts import Grid grid = Grid() grid.add(line, grid_right="10%") grid.add(pie, grid_left="10%") grid.render() """ from pyecharts import Scatter scatter = Scatter('散点图示例') scatter.add('test',v1,v2) page.add(scatter) from pyecharts import HeatMap import random x_axis = ['1a','2a','3a','4a','5a','6a','7a','8a','9a'] y_axis = ['Sat','Fri','Thu','Wed','Tue','Mon','Sun'] dataH = [[i,j,random.randint(0,50)] for i in range(9) for j in range(7)] heatmap = HeatMap('热力图示例') heatmap.add('test',x_axis,y_axis,dataH,is_visualmap = True,visual_text_color = "#000",visual_orient='horizontal') page.add(heatmap) from pyecharts import Kline dataK = [[2320.26,2320.33,2287.4,3513.4],[2340.26,2310.33,2187.4,2513.4],[2329.26,2420.33,2257.4,2573.4], [2321.26,2370.33,2187.4,3013.4],[2380.26,2220.33,2487.4,3113.4],[2350.26,2520.33,2087.4,2813.4], [2300.26,2390.33,2187.4,2413.4]] kline = Kline('K线图示例') kline.add('test',['2017/8/{}'.format(i+1) for i in range(7)],dataK) page.add(kline) from pyecharts import WordCloud dataW = {'Python':55,'HBASE':49,'JAVA':72,'MongoDB':88,'SAS':39,'Spark':63} wordcloud = WordCloud('词云图例',width = 1200,height = 720) wordcloud.add('test',dataW.keys(),dataW.values(),word_size_range = [20,80]) page.add(wordcloud) page.render(r'E:\echart.html')
参考:
pyechart : https://github.com/chenjiandongx/pyecharts/blob/master/docs/zh-cn/documentation.md
maplotlib : http://blog.csdn.net/xiaodongxiexie/article/details/53123371