The error is a simple mistake with your matplotlib code. You are plotting over your own image.
Where you have:
if box_plot:
plt.subplot(1, 1, 1)
plt.boxplot(data.data)
plt.subplot(1, 2, 2)
you need to specify the two rows of your subplots in both calls to plt.subplots
This will work.
if box_plot:
plt.subplot(1, 2, 1)
plt.boxplot(data.data)
plt.subplot(1, 2, 2)
If you want to size the plots independently then you can use gridspec. You might want to plot them above one another like this...
import numpy as np
from matplotlib import pyplot as plt
import matplotlib.gridspec as gridspec
def generate_data_heat_map(data, x_axis_label, y_axis_label, plot_title, file_path, box_plot=False):
plt.figure()
gs = gridspec.GridSpec(2, 1,height_ratios=[1,4])
if box_plot:
plt.subplot(gs[0])
plt.boxplot(data.data.flatten(), 0, 'rs', 0)
plt.subplot(gs[1])
plt.title(plot_title)
fig = plt.imshow(data.data, extent=[0, data.cols, data.rows, 0])
plt.xlabel(x_axis_label)
plt.ylabel(y_axis_label)
plt.colorbar(fig)
plt.savefig(file_path + '.png')
plt.close()
class Data(object):
def __init__(self, rows=200, cols=300):
# The data grid
self.cols = cols
self.rows = rows
# The 2D data structure
self.data = np.zeros((rows, cols), float)
def randomise(self):
self.data = np.random.rand(*self.data.shape)
data = Data()
data.randomise()
generate_data_heat_map(data, 'x', 'y', 'title', 'heat_map', box_plot=True)