请选择 进入手机版 | 继续访问电脑版
爱气象,爱气象家园! 

气象家园

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博登陆

只需一步, 快速开始

搜索
查看: 13621|回复: 3

[求助] python legend超出画布,还有如何将图横纵坐标互换

[复制链接]

新浪微博达人勋

发表于 2019-3-18 07:45:28 | 显示全部楼层 |阅读模式

登录后查看更多精彩内容~

您需要 登录 才可以下载或查看,没有帐号?立即注册 新浪微博登陆

x
画出来一张图,图例超出范围,保存效果如下图。求助怎么把图例包含在内。此外想把横坐标和纵坐标互换一下,纵坐标表示高度,横坐标表示风速,有什么办法,谢谢!
代码如下:
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Sun Mar 10 08:41:43 2019

  4. @author: chy
  5. """
  6. '''
  7. grads数据ctl文件
  8. dset ...\Dataquan/Fengsm.dat
  9. undef 9.999E+20
  10. xdef 1 linear   110 1
  11. ydef 1 linear   20 1
  12. zdef 127 levels 100 200 300 400 500 600 700 800 900 1000 1100 1200 1300 1400
  13. 1500 1600 1700 1800 1900 2000 2100 2200 2300 2400 2500 2600 2700 2800 2900
  14. 3000 3100 3200 3300 3400 3500 3600 3700 3800 3900 4000 4100 4200 4300 4400
  15. 4500 4600 4700 4800 4900 5000 5100 5200 5400 5600 5800 6000 6200 6400 6600
  16. 6800 7000 7200 7400 7600 7800 8000 8200 8400 8600 8800 9000 9200 9400 9600
  17. 9800 10000 10200 10400 10600 10800 11000 11200 11400 11600 11800 12000
  18. 12200 12400 12600 12800 13000 13200 13400 13600 13800 14000 14200 14400
  19. 14600 14800 15000 15200 15400 15600 15800 16000 16200 16400 16600 16800
  20. 17000 17200 17400 17600 17800 18000 18200 18400 18600 18800 19000 19200
  21. 19400 19600 19800 20000 20200
  22. tdef 45 linear 15aug2014 1mo
  23. vars 1
  24. Fengsm  127 99   
  25. ENDVARS
  26. '''

  27. '''
  28. 导入numpy,matplotlib
  29. '''
  30. import numpy as np
  31. #import matplotlib
  32. from matplotlib import pyplot as plt

  33. #from pylab import *  等效于 from matplotlib import pyplot as plt

  34. '''
  35. 中文字体
  36. '''
  37. #zhfont1=matplotlib.font_manager.FontProperties(fname="C:\py_font\SimHei.ttf")
  38. plt.rcParams['font.sans-serif'] = ['SimHei']  #全局修改中文,使用黑体
  39. '''
  40. 读取文件
  41. '''
  42. f=open('E:\paper\Grads\Dataquan\Fengsm.dat',mode='rb')
  43. fengs = np.fromfile(f,dtype='f')   #学习二进制数据读取,np.fromfile
  44. fengs1=fengs.reshape([45,127])  #reshape(t,z,y,x)
  45. #fengs.reshape([45,127])  #会出现reshape后的数组,但是原数组的不会变
  46. f.close()
  47. '''
  48. #获取127层高度
  49. '''
  50. hgt1=np.arange(100,5200,100)
  51. hgt2=np.arange(5200,20400,200)
  52. hgt=list(hgt1)+list(hgt2)
  53. #len(hgt)
  54. #print(hgt)

  55. '''
  56. 画图,用subplot多图,坐标轴设置,图例
  57. '''
  58. fig=plt.figure()
  59. fig,(ax1,ax2,ax3)=plt.subplots(1,3,sharex='row',sharey='all')


  60. for i in np.arange(5,17,1):
  61.     ax1.plot(hgt,fengs1[i,0:])
  62.     ax1.set_ylabel('风速')    #y轴
  63.     ax1.set_title('2015年风速变化')  #标题

  64. for i in np.arange(17,29,1):
  65.     ax2.plot(hgt,fengs1[i,0:])
  66.     ax2.set_title('2016年风速变化')  #标题
  67.     ax2.set_xlabel('高度')    #x轴

  68. for i in np.arange(29,41,1):
  69.     ax3.plot(hgt,fengs1[i,0:])
  70.     ax3.set_title('2017年风速变化')  #标题
  71.    

  72. #fig.set_subtitle('2015-2017年风速变化')
  73. fig.subplots_adjust(wspace=0.1)   #调整加宽不同子图之间的高间距,宽间距用wspace
  74. fig.legend(['一月','二月','三月','四月','五月','六月','七月','八月',
  75.                 '九月','十月','十一月','十二月'],\
  76.            bbox_to_anchor=(1.05, 0.8),ncol=1)  #添加图例
  77. plt.savefig('fengV.svg',bbox_inches = 'tight')
  78. plt.show()
复制代码


fengV.jpg
111.jpg
密码修改失败请联系微信:mofangbao

新浪微博达人勋

发表于 2019-3-18 08:45:24 | 显示全部楼层
可以试试把plt.savefig('fengV.svg',bbox_inches = 'tight')中的
bbox_inches = 'tight'去掉
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2019-3-18 12:32:01 | 显示全部楼层
Masterpiece 发表于 2019-3-18 08:45
可以试试把plt.savefig('fengV.svg',bbox_inches = 'tight')中的
bbox_inches = 'tight'去掉

不行,没有任何变化。还是有十一十二月的月字有一部分超出画布
密码修改失败请联系微信:mofangbao

新浪微博达人勋

 楼主| 发表于 2019-3-18 23:30:37 | 显示全部楼层
已经解决了,可以看看这几个参数,调整一下就OK了,主要是画legend的时候,超出画布。
borderpad : float or None
The fractional whitespace inside the legend border. Measured in font-size units. Default is None, which will take the value from rcParams["legend.borderpad"].

labelspacing : float or None
The vertical space between the legend entries. Measured in font-size units. Default is None, which will take the value from rcParams["legend.labelspacing"].

handlelength : float or None
The length of the legend handles. Measured in font-size units. Default is None, which will take the value from rcParams["legend.handlelength"].

handletextpad : float or None
The pad between the legend handle and text. Measured in font-size units. Default is None, which will take the value from rcParams["legend.handletextpad"].

borderaxespad : float or None
The pad between the axes and legend border. Measured in font-size units. Default is None, which will take the value from rcParams["legend.borderaxespad"].

columnspacing : float or None
The spacing between columns. Measured in font-size units. Default is None, which will take the value from rcParams["legend.columnspacing"].
密码修改失败请联系微信:mofangbao
您需要登录后才可以回帖 登录 | 立即注册 新浪微博登陆

本版积分规则

Copyright ©2011-2014 bbs.06climate.com All Rights Reserved.  Powered by Discuz! (京ICP-10201084)

本站信息均由会员发表,不代表气象家园立场,禁止在本站发表与国家法律相抵触言论

快速回复 返回顶部 返回列表