[可视化学习笔记]——02——折线图

Posted on   South_snow  阅读(93)  评论(0编辑  收藏  举报

一、读取execl数据画Double_Y_axis图

  选择折线图背景样式种类

复制代码
plt.style.use('fivethirtyeight')
######种类
print(plt.style.available) # 打印样式列表
['bmh', 'classic', 'dark_background', 'fast', 
'fivethirtyeight', 'ggplot', 'grayscale', 'seaborn-bright', 
'seaborn-colorblind', 'seaborn-dark-palette', 'seaborn-dark', 'seaborn-darkgrid',
 'seaborn-deep', 'seaborn-muted', 'seaborn-notebook', 'seaborn-paper', 
'seaborn-pastel', 'seaborn-poster', 'seaborn-talk', 'seaborn-ticks',
 'seaborn-white', 'seaborn-whitegrid', 'seaborn', 'Solarize_Light2', 
'tableau-colorblind10', '_classic_test']
复制代码

  具体类别样式可以看这篇回答  样式美化matplotlib.pyplot.style.use定制画布风格 - 知乎 (zhihu.com)  

  

复制代码
  1 import pandas as pd
  2 import matplotlib.pyplot as plt
  3 import numpy as np
  4 
  5 
  6 data_dir='/Users/zhongwangwei/Desktop'
  7 data = pd.read_excel('/home/kwang/PY/keshihua/plot_doubleY/test.xlsx')
  8 color = ("#51C1C8", "#E96279", "#44A2D6", "#536D84",
  9          "#51C1C8", "#E96279", "#44A2D6", "#536D84",
 10          "#51C1C8", "#E96279", "#44A2D6", "#536D84",
 11          "#51C1C8")
 12 year = data.index.to_list()
 13 year_color = dict(zip(year,color))          #元组转成字典
 14 year_color
 15 plt.style.use('fivethirtyeight')            #这只是style里面的一个种类,还有其他类别
 16 fig,ax = plt.subplots(figsize=(8,4),dpi=200,facecolor='white',edgecolor='white')
 17 ax.set_facecolor('white')
 18 
 19 x = np.arange(0,len(data),1)
 20 y = data['data01'].values
 21 
 22 #绘制连接点的线
 23 line = ax.plot(x,y,color='#333333',lw=1.,zorder=2)
 24 #绘制不同散点图
 25 scatter_out = ax.scatter(x,y,s=500,zorder=1,color='white',ec='grey',alpha=.7,lw=.5)
 26 for i in data.index.to_list():
 27     scatter = ax.scatter(x[i],y[i],s=180,zorder=3,ec='k',lw=.4,color=year_color[i])
 28 scatter_in = ax.scatter(x,y,s=30,zorder=3,color="#333333")
 29 
 30 #定制化绘制(设置图表风格)
 31 ax.grid(color='gray',lw=.5,alpha=.5) #设置网格
 32 ax.tick_params(left=False,bottom=False,labelbottom=False,labelsize=10,colors='gray')#设置刻度
 33 ax.set_ylim(bottom=-3,top=43)#设置轴范围
 34 ax.set_yticks(np.arange(0, 45, step=5)) #设置刻度标签
 35 ax.set_xticks(np.arange(-.5, 8, step=.5))
 36 #添加横线(修饰)
 37 ax.axhline(y=0,color='#45627C',lw=3)
 38 #添加数字标签
 39 label_text = {"size":13,"color":"k",'weight':'semibold'}
 40 for a,b in zip(x,y):
 41     ax.text(a, b+2.5, '%.0f' % b, ha='center', va= 'bottom',fontdict=label_text,color=year_color[a])
 42 #设置轴脊(spine)
 43 for spine in ['top','bottom','left','right']:
 44     ax.spines[spine].set_color("#FFFFFF") #设置颜色/set_visible()设置显示与否
 45 #for i in data.index.to_list()[:3]:
 46 #    axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
 47 '''    
 48 #添加标题处小图
 49 #添加小散点图:重点掌握   
 50 axins = inset_axes(ax, width=.4, height=.4,loc='upper left',
 51                    bbox_to_anchor=(0.01, 0.22, 1, 1),
 52                    bbox_transform=ax.transAxes,
 53                    borderpad=0)
 54 axins.set_ylim(bottom=8,top=35)
 55 axins.set_xlim(left=-.5,right=2.5)
 56 axins.plot(x[:3],y[:3],color='#333333',lw=1.,zorder=1)
 57 for i in data.index.to_list()[:3]:
 58     axins.scatter(x[i],y[i],s=80,color=year_color[i],zorder=2)
 59 axins.axis('off')
 60 #绘制小横线:原理同上
 61 line = inset_axes(ax,width=5.3, height=.4,loc='upper left',
 62                   bbox_to_anchor=(-0.015, 0.15, 1, 1),
 63                   bbox_transform=ax.transAxes,
 64                   borderpad=0)
 65 line.plot([.1,.7],[.1,.1],color='#45627C',lw=2)
 66 line.axis('off')
 67 '''
 68 #添加阴影效果
 69 for i in data.index.to_list():
 70     ax.axvspan(i-.35, i+.35, facecolor='gray',alpha=.1,zorder=0)
 71     
 72 #添加双y轴:使用Axes.twinx()方法绘制
 73 second_plot = ax.twinx()
 74 second_plot.set_ylim(bottom=-3,top=43)
 75 second_plot.set_yticks(np.arange(0, 50, step=10))
 76 second_plot.set_xticks(np.arange(-.5, 8, step=.5))
 77 second_plot.tick_params(left=False,bottom=False,labelbottom=False,labelsize=10,colors='k')
 78 second_plot.grid(color="none",zorder=0)
 79 second_plot.set_axisbelow(True)
 80 for spine in ['top','bottom','left','right']:
 81     second_plot.spines[spine].set_visible(False) #("#FFFFFF")
 82 
 83 y2 = data['data02'].values
 84 label_text = {"size":28,"color":"white",'weight':'light'}
 85 for x,y2 in zip(np.arange(len(data)).tolist(),data['data02'].to_list()):
 86     second_plot.plot([x,x],[0,y2],lw=20,color=color[x],solid_capstyle='round')
 87     #绘制空心圆
 88     second_plot.scatter(x,0,s=150,c='white',zorder=3)
 89     second_plot.scatter(x,0,s=60,c=color[x],zorder=4)
 90     second_plot.scatter(x,0,s=15,c='white',zorder=5)
 91    
 92 # 添加文本信息
 93 label_font = {"size":15,'weight':'bold'}
 94 for i,x,text in zip(data.index.to_list(),np.arange(0,len(data),1),data['year'].values):
 95     ax.text(x, -8,text ,ha='center', va= 'bottom',fontdict=label_font,color=year_color[i],zorder=2)
 96 '''
 97 ax.text(.39,1.2,'\nSecond Y Axes Plot Exercise',transform = ax.transAxes,
 98         ha='center', va='center',fontsize = 20,color='k',fontweight="bold")
 99 ax.text(.02,1.04,'Use the Matplotlib axes.Axes.twinx()',
100         transform = ax.transAxes,
101         ha='left', va='center',fontsize = 9,color='#45627C')
102 
103 ax.text(.91,.02,'\nVisualization by DataCharm',transform = ax.transAxes,
104         ha='center', va='center',fontsize = 7,color='black')
105 '''
106 plt.savefig(r'/home/kwang/PY/keshihua/plot_doubleY/double_y_axis_plot.png',width=6,height=3,
107             dpi=900,bbox_inches='tight',facecolor='white')
108 #ax.set_axisbelow(True)
109 plt.show()
复制代码

  后面的接下来再慢慢试。

  

 

  

 

相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 记一次.NET内存居高不下排查解决与启示
点击右上角即可分享
微信分享提示