Search code examples
pythonnumpymatplotlibdimensions

Numpy Array Dimension Issue


I have two arrays which have different dimensions. I want to bring the latter(india_cases2) to the former(baseline_diag) so that I can plot them easily. I'm unable to plot india_cases.

india_daily_cases = pd.read_csv('India_Cases.csv')
india_daily_cases_subset = india_daily_cases.loc[india_daily_cases['t']> 33.,['Daily Confirmed']]
india_cases = india_daily_cases_subset.to_numpy()
india_cases2 = india_cases.T

The baseline_diag is an output of function - not sure if providing the whole function code is of any help here. The difference in dimension is as follows:

enter image description here

The code to plot the graphs is follows:

ax.plot(t, india_cases.T, 'r', alpha=0.5, lw=2, label='Actual Observed Cases')
ax.bar(t - 0.2, baseline_onsets, 0.4, label='New Onsets')
ax.bar(t + 0.2, baseline_cases, 0.4, label='New Cases')

The error I get is follows:

ValueError: x and y must have same first dimension, but have shapes (47,) and (1, 47)

How do I change the dimenion for india_cases2


Solution

  • There are a number of equivalent methods you can use:

    india_cases2.ravel()
    
    np.squeeze(india_cases2)
    
    india_cases2.reshape(-1)
    

    Even something like

    india_cases2.shape = (india_cases2.size,)
    

    Alternatively, you could expand t with

    t.reshape(1, -1)