在上篇 Matplotlib 数据可视化教程中,我们要引见怎样建立堆叠图和饼图。本日给人人带来是针对于图形色彩和线条的添补。
色彩
我们要做的第一个修正是将plt.title变动成stock变量。
plt.title(stock)
如今,让我们来引见一下怎样变动标签色彩。 我们能够经由过程修正我们的轴对象来完成:
ax1.xaxis.label.set_color('c') ax1.yaxis.label.set_color('r')
假如我们运转它,我们会看到标签改变了色彩,就像在单词中那样。
接下来,我们能够为要显现的轴指定细致数字,而不是像如许的自动挑选:
ax1.set_yticks([0,25,50,75])
接下来,我想引见添补。 添补所做的事变,是在变量和你挑选的一个数值之间添补色彩。 比方,我们能够如许:
ax1.fill_between(date, 0, closep)
所以到这里,我们的代码为:
import matplotlib.pyplot as plt import numpy as np import urllib import datetime as dt import matplotlib.dates as mdates def bytespdate2num(fmt, encoding='utf-8'): strconverter = mdates.strpdate2num(fmt) def bytesconverter(b): s = b.decode(encoding) return strconverter(s) return bytesconverter def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1,1), (0,0)) stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source: split_line = line.split(',') if len(split_line) == 6: if 'values' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')}) ax1.fill_between(date, 0, closep) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.grid(True)#, color='g', linestyle='-', linewidth=5) ax1.xaxis.label.set_color('c') ax1.yaxis.label.set_color('r') ax1.set_yticks([0,25,50,75]) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock) plt.legend() plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) plt.show() graph_data('EBAY')
效果为:
添补的一个问题是,我们能够末了会把东西都掩盖起来。 我们能够用透明度来处置惩罚它:
ax1.fill_between(date, 0, closep)
如今,让我们引见前提添补。 让我们假定图表的肇端位置是我们最先买入 eBay 的处所。 这里,假如价钱低于这个价钱,我们能够向上添补到本来的价钱,然后假如它超过了原始价钱,我们能够向下添补。 我们能够如许做:
ax1.fill_between(date, closep[0], closep)
会生成:
假如我们想用赤色和绿色添补来展现收益/丧失,那末与原始价钱比拟,绿色添补用于上升(注:外洋股市的色彩和国内相反),赤色添补用于下跌? 没问题! 我们能够增加一个where参数,以下所示:
ax1.fill_between(date, closep, closep[0],where=(closep > closep[0]), facecolor='g', alpha=0.5)
这里,我们添补当前价钱和原始价钱之间的地区,个中当前价钱高于原始价钱。 我们赋予它绿色的前景色,由于这是一个上升,而且我们运用细小的透明度。
线条
我们依然不能对多边形数据(如添补)运用标签,但我们能够像之前一样完成空线条,因而我们能够:
ax1.plot([],[],linewidth=5, label='loss', color='r',alpha=0.5) ax1.plot([],[],linewidth=5, label='gain', color='g',alpha=0.5) ax1.fill_between(date, closep, closep[0],where=(closep > closep[0]), facecolor='g', alpha=0.5) ax1.fill_between(date, closep, closep[0],where=(closep < closep[0]), facecolor='r', alpha=0.5)
这向我们供应了一些添补,以及用于处置惩罚标签的空线条,我们盘算将其显现在图例中。这里完全的代码是:
import matplotlib.pyplot as plt import numpy as npimport urllib import datetime as dt import matplotlib.dates as mdates def bytespdate2num(fmt, encoding='utf-8'): strconverter = mdates.strpdate2num(fmt) def bytesconverter(b): s = b.decode(encoding) return strconverter(s) return bytesconverter def graph_data(stock): fig = plt.figure() ax1 = plt.subplot2grid((1,1), (0,0)) stock_price_url = 'http://chartapi.finance.yahoo.com/instrument/1.0/'+stock+'/chartdata;type=quote;range=10y/csv' source_code = urllib.request.urlopen(stock_price_url).read().decode() stock_data = [] split_source = source_code.split('\n') for line in split_source: split_line = line.split(',') if len(split_line) == 6: if 'values' not in line and 'labels' not in line: stock_data.append(line) date, closep, highp, lowp, openp, volume = np.loadtxt(stock_data, delimiter=',', unpack=True, converters={0: bytespdate2num('%Y%m%d')}) ax1.plot_date(date, closep,'-', label='Price') ax1.plot([],[],linewidth=5, label='loss', color='r',alpha=0.5) ax1.plot([],[],linewidth=5, label='gain', color='g',alpha=0.5) ax1.fill_between(date, closep, closep[0],where=(closep > closep[0]), facecolor='g', alpha=0.5) ax1.fill_between(date, closep, closep[0],where=(closep < closep[0]), facecolor='r', alpha=0.5) for label in ax1.xaxis.get_ticklabels(): label.set_rotation(45) ax1.grid(True)#, color='g', linestyle='-', linewidth=5) ax1.xaxis.label.set_color('c') ax1.yaxis.label.set_color('r') ax1.set_yticks([0,25,50,75]) plt.xlabel('Date') plt.ylabel('Price') plt.title(stock) plt.legend() plt.subplots_adjust(left=0.09, bottom=0.20, right=0.94, top=0.90, wspace=0.2, hspace=0) plt.show() graph_data('EBAY')
如今我们的效果是:
以上就是Matplotlib中对图形色彩和线条的添补的细致内容,更多请关注ki4网别的相干文章!