import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
from datetime import datetime
plt.rcParams["figure.figsize"] = (11, 5)
plt.rcParams['figure.constrained_layout.use'] = True
#sourced from https://news.gallup.com/poll/354638/approval-interracial-marriage-new-high.aspx
t_data = [
datetime(1958, 9, 29),
datetime(1968, 7, 1),
datetime(1972, 10, 16),
datetime(1978, 7, 24),
datetime(1983, 5, 2),
datetime(1991, 6, 16),
datetime(1994, 9, 20),
datetime(1997, 2, 28),
datetime(2002, 6, 9),
datetime(2003, 12, 14),
datetime(2004, 6, 30),
datetime(2007, 6, 24),
datetime(2007, 9, 8),
datetime(2011, 8, 7),
datetime(2013, 7, 5),
datetime(2021, 7, 21)
]
approve_data = np.array([4, 20, 29, 36, 43, 48, 48, 64, 65, 73, 76, 77, 79, 86, 87, 94])
disapprove_data = np.array([94, 73, 60, 54, 50, 42, 37, 27, 29, 23, 19, 17, 15, 11, 11, 4])
no_opinion_data = np.array([3, 8, 11, 10, 7, 10, 15, 9, 6, 4, 5, 6, 6, 3, 2, 2])
plt.suptitle("Approval of interracial marriage between Black and White people in the United States")
plt.title("Source: Gallup, Inc. surveys")
plt.xlim(datetime(1958, 1, 1), datetime(2022, 12, 31))
plt.xticks([datetime(n, 1, 1) for n in np.arange(1958, 2022, 3)])
plt.xlabel("Year")
ax = plt.gca()
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y"))
plt.ylim(0, 100)
plt.ylabel("% of survey respondents")
plt.plot(t_data, approve_data, label = "Approve", marker = "o", color = "red")
plt.plot(t_data, disapprove_data, label = "Disapprove", marker = "o", color = "blue")
plt.plot(t_data, no_opinion_data, label = "No opinion", marker = "o", color = "green")
plt.legend()
plt.grid(True, axis = "y")
plt.savefig("Public opinion of interracial marriage in the United States 2021.svg")
plt.show()