提示:以下是本篇文章正文内容,下面案例可供参考
一、数据
二、matplotlib
代码:
from pandas import read_csv
import pandas
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.font_manager import FontProperties
#设置中文字体介绍两种方法
#方法一(简短,适用于折线图柱形图等):
#import matplotlib as mpl
#mpl.rcParams['font.sans-serif'] = ['SimHei']
#方法二:font_set(适用于折线图,代码多)
font_set = FontProperties(fname=r"c:\windows\fonts\simsun.ttc", size=15)
filename='D:\datasets\mldata\Revenue.xlsx'
rd = pandas.read_excel(filename,header=0,names=["银行","2015","2016","2017","2018","2019"])
data = np.array(rd)
name=data[0][0]
time = ["2015","2016","2017","2018","2019"]
revenue = data[0,1:]
plt.figure(figsize = (15,7),dpi = 80)
plt.plot(time,revenue)
plt.title("中国银行2015-2019年营业收入折线图")
plt.xlabel("年份",fontproperties=font_set)
plt.ylabel('营业收入(单位:亿)',fontproperties=font_set)
for a, b in zip(time, revenue):
plt.text(a, b, b,ha='right', va='bottom', fontsize=12) #ha:水平对齐方式 va:垂直对齐方式
for spine in plt.gca().spines.keys(): #去掉右边和上边的框
if spine == 'top' or spine == 'right':
plt.gca().spines[spine].set_color('none')
plt.show()
运行效果:
二、pyecharts
代码:
import pyecharts.options as opts
from pandas import read_csv
from pyecharts.charts import Line
import pandas
import numpy as np
filename='D:\datasets\mldata\Revenue.xlsx'
rd = pandas.read_excel(filename,header=0,names=["银行","2015","2016","2017","2018","2019"])
data = np.array(rd)
name=data[0][0]
time = ["2015","2016","2017","2018","2019"]
revenue = data[0,1:]
line = Line()
line.add_xaxis(xaxis_data=time)
line.set_global_opts(
xaxis_opts=opts.AxisOpts(name='年份'),
yaxis_opts=opts.AxisOpts(name='营业收入(单位:亿)', min_=4600),
title_opts=opts.TitleOpts(title='中国银行2015~2019年收入'),
legend_opts=opts.LegendOpts(is_show=True),# is_show = True 默认-展示图例
tooltip_opts=opts.TooltipOpts(trigger='axis',axis_pointer_type='cross') #添加水平垂直线
)
line.add_yaxis(series_name='收入',y_axis=revenue)
line.render_notebook()
运行效果:
三、对比
很显然pyecharts相比matplotlib代码更简洁,相对动态,且支持中文