matplotlib同x轴作图,y轴反转

matplotlib同x轴作图,y轴反转
编辑删除转载2018-10-08 15:08:09
标签:linux
stackoverflow.com/questions/37737538/merge-matplotlib-subplots-with-shared-x-axis 

这里给了个例子,用了gridspec,其实只用subplot就可以了。


ax1=plt.subplot(211)

plt.vlines(tm1,0,mg1,color='r')

plt.xlim((1700,2020))

plt.ylim((0,8))

plt.setp(ax1.get_xticklabels(),visible=False)  ###隐藏第一个图的x轴标注



ax2=plt.subplot(212,sharex=ax1)

plt.vlines(tm2,0,mg2,color='b')

plt.xlim((1700,2020))

plt.ylim((8,0))   ##### 这里反着写,让y轴倒过来

yticks=ax2.yaxis.get_major_ticks()

yticks[0].label1.set_visible(False)  #### 两个图相交的y轴第一个标注会重叠,让第二个坐标的标注隐藏

plt.subplots_adjust(hspace=.0)    ##### 让两个图的间隔为0

matplotlib画条状时间序列图

想画一个台站的运行状态图像,如果某天台站正常运行,则在该天用方框标识为红色,否则就空在那里,因此该图像是个时间序列的条状图。

在这里找到了解决方案

stackoverflow.com/questions/44951911/plot-a-binary-timeline-in-maplotlib

用的是broken_barh函数

要注意的问题有:

这个函数貌似不认识日期类型的数据,因此要将日期转为数值,这里用到了

matplotlib.dates.date2num

但如果直接出图,会发现x轴坐标是数值而不是日期,因此还要对x轴的属性进行修改,类似于这样的语句

ax.xaxis.set_major_locator(matplotlib.dates.MonthLocator())

ax.xaxis.set_minor_locator(matplotlib.dates.YearLocator())



broken_barh函数主要包含x和y两个参数,其中x的构成是

(start,offset)

也可以是由这种tuple构成的list。画图的时候方框在x轴的起点是start,终点是start+offset

参数y类似,但如果高度固定,给定一个tuple就够了,可以如下处理

inxval=matplotlib.dates.date2num(test_ser)

times=list(zip(inxval,np.ones(len(test_ser))))

plt.broken_barh(times,(-1,1),color='red')

matlab调用gfortran编译的fortran程序出现end of file问题

https: // ww2.mathworks.cn/matlabcentral/answers/44388-or-system-or-unix-input-redirection#answer_56185

Executing a GFORTRAN executable within a Apple terminal window is not equivalent to executing it within MATLAB. In particular, MATLAB will set various environment variables and this can have unexpected results which seems to be the case here.

To work around this issue you need to set your environment variables for the GFORTRAN-compiled programs correctly within MATLAB before you execute them. You can use the SETENV function for this purpose. Please execute your FORTRAN script as follows:

setenv(‘GFORTRAN_STDIN_UNIT’, ‘5’)
setenv(‘GFORTRAN_STDOUT_UNIT’, ‘6’)
setenv(‘GFORTRAN_STDERR_UNIT’, ‘0’)
!./forward <
input
setenv(‘GFORTRAN_STDIN_UNIT’, ‘-1’)
setenv(‘GFORTRAN_STDOUT_UNIT’, ‘-1’)
setenv(‘GFORTRAN_STDERR_UNIT’, ‘-1’)

Note that after executing the FORTRAN script, I set the environment variables back to their default values.

Fatal error in MPI_Init:Other MPI error, error stack

一个MPI程序,出现这样的错误,仔细看看后续的提示行,是否出现如下的错误:gethostbyname failed
出现这种错误的原因是hosts文件中没有列入当前主机的hostname先看看主机的hostname是什么,用uname -a查看,比如我的是Linux scdzj 2.6.32-696.6.3.el6.x86_64 #1 SMP Wed Jul 12 14:17:22 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux则第二个字段scdzj是hostname打开/etc/hosts文件,在127.0.0.1的后面加上hostname,保存退出。这个问题即可解决。
stackoverflow. com/questions/23112515/mpich2-gethostbyname-failed

pandas的date_range时间偏置

想用pandas.data_range(start=’1970′,end=’2020′,freq=’Y’)来生成一个按年分布的时间序列,但实际结果是生成了这样的:1970-12-31,1971-12-31… …我需要的是每年开始的日期,经查找,发现可用如下办法:pandas.date_range(start=’1970′,end=’2020′,freq=pandas.offsets.YearBegin(1))即可。

===========================
也可以简单写成pandas.date_range(start=’1970′,end=’2020′,freq=’1YS’)

illustrator无法编辑matplotlib保存的矢量图中的文本

stackoverflow.com    /questions/5956182/cannot-edit-text-in-chart-exported-by-matplotlib-and-opened-in-illustrator

是由于默认的fonttype=3引起的,illustrator不能处理这种类型,可改为42(TrueType)就可以了:

>>> import matplotlib as mpl 

>>> mpl.rcParams['pdf.fonttype'] = 42