What is the best way to pad a numpy array and graph it?

Viewed 78

I have an numpy array that is 20 values long. y2020['no._of_cases'].shape = 20
I want to make it to be 52 values long, so that i can chart it with some blanks space behind.

I tried padding it with np.nan but it returns a large negative number which messes up the chart. I tried padding it with blanks spaces but that will cause tracebacks when charting.

 np.pad(y2020['no._of_cases'], (0,32),'constant', constant_values='np.nan')

Any pygod have any ideas?

1 Answers

Matplotlib plot the charts based on values. It wont plot extra blank space.

The result of pad has the same type as the input.

You can convert your input to float and then try np.nan. It will help.

np.pad(y2020['no._of_cases'].astype(float), (0,32), 'constant', constant_values=np.nan)

To plot it:

plt.plot(np.pad(y2020['no._of_cases'].astype(float), (0,32), 'constant', constant_values=np.nan))
plt.show()

If your concern is all about blank space behind it you can try padding with 0

plt.plot(np.pad(y2020['no._of_cases'].astype(float), (0,32), 'constant', constant_values=0))
plt.show()

Hope this will help.

Related