-1

I would like to plot SupDem (variable) where e_boix_regime==1 and SupDem where e_boix_regime==0.

My data:

year SupDem e_boix_regime
1997 0.98 1
1998 0.75 0

My code:

dem = dem_aut[dem_aut["e_boix_regime"]==1].SupDem
aut = dem_aut[dem_aut["e_boix_regime"]==0].SupDem
year = dem_aut["year"]
plt.plot(year, dem, label="Suuport for Democracy in Demcoracies")
plt.plot(year, aut, label="Support for Democracy in Autocracies")
plt.show()```

The error is follwoing: x and y must have same first dimension, but have shapes (53,) and (28,)

I just wanted to plot two lines together.
user19562955
  • 293
  • 1
  • 9

2 Answers2

0

This can help you solve the problem. I hope you can reproduce the codee with it: two (or more) graphs in one plot with different x-axis AND y-axis scales in python

0

Issue

Your issue is regarding shape of x and y. For plotting graph you need same data point/shape of x-values and y-values.

Solution

Take each year with dem_aut["e_boix_regime"]==1 and dem_aut["e_boix_regime"]==2 condition as you are doing with SupDem

Source Code

df = pd.DataFrame(
    {
        "SupDem": np.random.randint(1, 11, 30),
        "year": np.random.randint(10, 21, 30),
        "e_boix_regime": np.random.randint(1, 3, 30),
    }
) # see DataFrame below

df["e_boix_regime"].value_counts() # 1 = 18, 2 = 12

df[df["e_boix_regime"] == 2][["SupDem", "year"]] # see below

# you need same no. of data points for both x/y axis i.e. `year` and `SupDem`
plt.plot(
    df[df["e_boix_regime"] == 1]["year"], df[df["e_boix_regime"] == 1]["SupDem"], marker="o", label="e_boix_regime==1"
)
# hence applying same condition for grabbing year which is applied for SupDem
plt.plot(
    df[df["e_boix_regime"] == 2]["year"], df[df["e_boix_regime"] == 2]["SupDem"], marker="o", label="e_boix_regime==2"
)
plt.xlabel("Year")
plt.ylabel("SupDem")
plt.legend()
plt.show()

Output

Plot
PS: Ignore the data point plots, it's generated from random values

DataFrame Outputs

    SupDem  year  e_boix_regime
0        1    12              2
1       10    10              1
2        5    19              2
3        4    14              2
4        8    14              2
5        4    17              2
6        2    15              2
7       10    11              1
8        8    11              2
9        6    19              2
10       5    15              1
11       8    17              1
12       9    10              2
13       1    14              2
14       8    18              1
15       3    13              2
16       6    16              2
17       1    16              1
18       7    13              1
19       8    15              2
20       2    17              2
21       5    10              2
22       1    19              2
23       5    20              2
24       7    16              1
25      10    14              1
26       2    11              2
27       1    18              1
28       5    16              1
29      10    18              2
df[df["e_boix_regime"] == 2][["SupDem", "year"]] 

    SupDem  year
0        1    12
2        5    19
3        4    14
4        8    14
5        4    17
6        2    15
8        8    11
9        6    19
12       9    10
13       1    14
15       3    13
16       6    16
19       8    15
20       2    17
21       5    10
22       1    19
23       5    20
26       2    11
29      10    18
GodWin1100
  • 1,380
  • 1
  • 6
  • 14