我有一个类需要存储一个可变大小的数组.理想情况下,此大小将被定义为给予类的构造函数的参数.
我可以定义一个常量然后使用它,如下所示:
#include <iostream>
#define ARRSIZE 5
class Classy{
private:
int myarray[ARRSIZE];
public:
Classy();
void printarray();
};
Classy::Classy(){
for(int i = 0; i < ARRSIZE; i++){
myarray[i] = i * i * 2;
}
}
void Classy::printarray(){
for(int i = 0; i < ARRSIZE; i++){
std::cout << myarray[i] << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
但是,我想这样做:
#include <iostream>
class Classy{
private:
int arraysize;
int myarray[arraysize];
public:
Classy(int parraysize);
void printarray();
};
Classy::Classy(int parraysize){
arraysize = parraysize;
for(int i = 0; i < arraysize; …Run Code Online (Sandbox Code Playgroud) 所以我试图在共享x轴的同一图中绘制两个子图.但是,我无法得到它来绘制最后一个小xtick.我不知道这种行为来自哪里,但我设法用随机数据重现它.
使用的系统是python2.7和matplotlib v1.2.1
所以这里是我的最小错误再现示例:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from matplotlib.ticker import MaxNLocator
xdat = np.linspace(0,6.6,endpoint=True)
ydat1 = np.random.rand(50)*500
ydat2 = np.random.rand(50)*4
fig = plt.figure(figsize=(6,8), dpi=72)
gs = gridspec.GridSpec(2,1, height_ratios=[3,1])
fig.subplots_adjust(hspace=0.0)
ax1 = plt.subplot(gs[0])
ax1.plot(xdat, ydat1)
ax1.set_xlim(0,6.6)
ax1.set_xticks(range(0,8,1))
ax1.minorticks_on()
[label.set_visible(False) for label in ax1.get_xticklabels() ] # Make tick labels invisible
ax2 = plt.subplot(gs[1], sharex=ax1)
ax2.plot(xdat, ydat2, 'r-')
ax2.yaxis.set_major_locator(MaxNLocator(nbins=5, steps=[1,2,4,5,10], symmetric=False, prune='upper'))
plt.show()
Run Code Online (Sandbox Code Playgroud)
我得到了以下图像:

我不知道我是否发现了一个错误,或者是否有一种简单的方法来缓解这个问题(即更新matplotlib).