-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
208 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import pandas as pd | ||
import os | ||
import plotly.graph_objects as go | ||
from plotly.subplots import make_subplots | ||
|
||
def plot_policy_percentages(csv_file, output_path): | ||
# Check the csv file | ||
if not os.path.exists(csv_file): | ||
raise FileNotFoundError(f"The file {csv_file} does not exist.") | ||
|
||
# Load the data | ||
df = pd.read_csv(csv_file) | ||
df['year_month'] = pd.to_datetime(df['year_month']) | ||
|
||
# Create the figure | ||
fig = make_subplots(specs=[[{"secondary_y": False}]]) | ||
|
||
# Add traces for each policy area | ||
for column in df.columns: | ||
if column != 'year_month': | ||
fig.add_trace( | ||
go.Scatter(x=df['year_month'], | ||
y=df[column], | ||
name=column, | ||
mode='lines', | ||
visible=True) | ||
) | ||
|
||
# Update layout | ||
fig.update_layout( | ||
# title='Percentage of Articles by Policy Area Over Time', | ||
# xaxis_title='Date', | ||
yaxis_title='Monthly Percentage of Articles per Policy Area', | ||
legend_title='Policy Area', | ||
hovermode='x unified', | ||
xaxis=dict( | ||
tickformat='%b-%Y', | ||
tickmode='auto', | ||
nticks=20, | ||
), | ||
yaxis=dict( | ||
tickformat='.1f', | ||
ticksuffix='%'), | ||
|
||
) | ||
|
||
# Add buttons for selecting all or none | ||
fig.update_layout(dict(updatemenus=[ | ||
dict( | ||
type = "buttons", | ||
direction = "left", | ||
buttons=list([ | ||
dict( | ||
args=["visible", "legendonly"], | ||
label="Unselect All", | ||
method="restyle" | ||
), | ||
dict( | ||
args=["visible", True], | ||
label="Select All", | ||
method="restyle" | ||
) | ||
]), | ||
pad={"r": 10, "t": 10}, | ||
showactive=False, | ||
x=1.25, | ||
xanchor="right", | ||
y=1.05, | ||
yanchor="top" | ||
), | ||
] | ||
)) | ||
|
||
|
||
# Add vertical line at 2022-01 with hover text | ||
fig.add_shape( | ||
type="line", | ||
x0="2022-01-01", | ||
y0=0, | ||
x1="2022-01-01", | ||
y1=1, | ||
line=dict(color="Gray", width=2, dash="dash"), | ||
xref='x', | ||
yref='paper' | ||
) | ||
|
||
fig.add_annotation( | ||
x="2022-01-01", | ||
y=0.90, | ||
xref='x', | ||
yref='paper', | ||
text="<b>Jan 2022</b><br>Model prediction starts", | ||
showarrow=True, | ||
arrowhead=2, | ||
arrowsize=1, | ||
arrowwidth=2, | ||
arrowcolor="#FF4500", # OrangeRed color | ||
ax=0, | ||
ay=-40, | ||
bordercolor="#FF4500", | ||
borderwidth=2, | ||
borderpad=4, | ||
bgcolor="#FF4500", | ||
opacity=0.8, | ||
font=dict(color="white", size=12), | ||
align="center" | ||
) | ||
|
||
# Configure the legend to always show and allow single item toggle | ||
fig.update_layout( | ||
legend=dict( | ||
itemsizing='constant', | ||
itemclick='toggle', | ||
itemdoubleclick='toggleothers' | ||
)) | ||
|
||
# Save the plot as an HTML file | ||
fig.write_html( | ||
output_path, | ||
full_html=True, | ||
include_plotlyjs='cdn' | ||
) | ||
|
||
# Use the funciont | ||
if __name__ == "__main__": | ||
csv_file = 'pci_nko_fg2_df.csv' | ||
output_path = 'plot2.html' | ||
plot_policy_percentages(csv_file, output_path) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
year_month,Agriculture and Food Security,Defense and National Security,Economic and Industrial Policy,Education and Human Capital,Foreign Relations and Diplomacy,Infrastructure and Public Works,Labor and Employment,Public Health and Safety,Science and Technology,Social Policy and Ideology | ||
2018-01,8.552631578947368,3.289473684210526,37.5,2.631578947368421,13.815789473684212,5.263157894736842,0.0,0.0,4.605263157894736,24.342105263157894 | ||
2018-02,6.508875739644971,5.9171597633136095,30.76923076923077,1.183431952662722,19.526627218934912,3.5502958579881656,0.0,1.7751479289940828,1.7751479289940828,28.994082840236686 | ||
2018-03,11.351351351351353,1.6216216216216217,38.91891891891892,1.6216216216216217,14.054054054054054,7.567567567567568,0.0,0.0,2.1621621621621623,22.702702702702705 | ||
2018-04,9.554140127388536,0.6369426751592357,28.662420382165603,1.910828025477707,27.388535031847134,6.369426751592357,0.0,0.6369426751592357,0.6369426751592357,24.203821656050955 | ||
2018-05,11.65644171779141,2.4539877300613497,41.104294478527606,1.2269938650306749,17.177914110429448,9.815950920245399,0.0,1.2269938650306749,1.2269938650306749,14.11042944785276 | ||
2018-06,9.868421052631579,1.9736842105263157,39.473684210526315,2.631578947368421,15.789473684210526,10.526315789473683,0.0,0.6578947368421052,1.9736842105263157,17.105263157894736 | ||
2018-07,12.903225806451612,0.0,37.41935483870968,0.0,9.032258064516128,9.032258064516128,0.6451612903225806,2.5806451612903225,1.2903225806451613,27.09677419354839 | ||
2018-08,10.06711409395973,0.6711409395973155,46.308724832214764,3.3557046979865772,10.738255033557047,9.395973154362416,0.0,0.6711409395973155,2.684563758389262,16.10738255033557 | ||
2018-09,7.18954248366013,1.9607843137254901,34.64052287581699,1.3071895424836601,26.143790849673206,9.15032679738562,0.0,0.0,1.3071895424836601,18.30065359477124 | ||
2018-10,10.7981220657277,0.0,41.31455399061033,2.8169014084507045,13.145539906103288,8.92018779342723,0.4694835680751174,1.4084507042253522,1.4084507042253522,19.718309859154928 | ||
2018-11,10.919540229885058,0.5747126436781609,40.229885057471265,0.5747126436781609,9.195402298850574,11.494252873563218,0.0,3.4482758620689653,2.8735632183908044,20.689655172413794 | ||
2018-12,12.903225806451612,4.301075268817205,35.483870967741936,2.1505376344086025,5.376344086021505,9.13978494623656,0.0,0.0,3.225806451612903,27.419354838709676 | ||
2019-01,8.552631578947368,2.631578947368421,32.89473684210527,1.9736842105263157,15.789473684210526,9.868421052631579,0.0,1.3157894736842104,1.9736842105263157,25.0 | ||
2019-02,8.670520231213873,4.624277456647398,21.965317919075144,2.8901734104046244,12.716763005780345,6.358381502890173,0.0,0.5780346820809248,4.046242774566474,38.15028901734104 | ||
2019-03,11.834319526627219,0.591715976331361,25.443786982248522,3.5502958579881656,13.017751479289942,10.650887573964498,0.0,0.591715976331361,1.7751479289940828,32.544378698224854 | ||
2019-04,12.037037037037036,4.62962962962963,22.22222222222222,0.0,17.59259259259259,9.25925925925926,0.0,0.9259259259259258,1.8518518518518516,31.48148148148148 | ||
2019-05,21.390374331550802,2.13903743315508,22.459893048128343,1.6042780748663104,15.508021390374333,13.903743315508022,0.0,0.0,3.2085561497326207,19.786096256684495 | ||
2019-06,16.666666666666664,0.641025641025641,23.717948717948715,1.282051282051282,13.461538461538462,10.897435897435898,0.0,0.0,2.564102564102564,30.76923076923077 | ||
2019-07,13.20754716981132,3.1446540880503147,25.157232704402517,2.515723270440252,11.949685534591195,9.433962264150944,1.8867924528301887,1.8867924528301887,2.515723270440252,28.30188679245283 | ||
2019-08,13.194444444444445,4.861111111111112,29.86111111111111,2.083333333333333,8.333333333333332,11.805555555555555,1.3888888888888888,0.6944444444444444,2.083333333333333,25.694444444444443 | ||
2019-09,11.176470588235295,0.5882352941176471,14.705882352941178,12.941176470588237,14.705882352941178,17.647058823529413,0.0,1.1764705882352942,1.7647058823529411,25.294117647058822 | ||
2019-10,12.307692307692308,7.6923076923076925,12.307692307692308,0.7692307692307693,16.923076923076923,14.615384615384617,0.0,0.7692307692307693,4.615384615384616,30.0 | ||
2019-11,16.10738255033557,2.013422818791946,19.463087248322147,2.684563758389262,11.409395973154362,15.436241610738255,0.0,0.6711409395973155,2.013422818791946,30.201342281879196 | ||
2019-12,16.793893129770993,1.5267175572519083,8.396946564885496,0.7633587786259541,16.793893129770993,6.870229007633588,0.0,0.7633587786259541,3.0534351145038165,45.038167938931295 | ||
2020-01,10.81081081081081,2.7027027027027026,27.927927927927925,0.0,7.207207207207207,7.207207207207207,0.0,3.6036036036036037,0.0,40.54054054054054 | ||
2020-02,8.0,1.3333333333333335,29.333333333333332,3.3333333333333335,10.0,8.0,0.0,3.3333333333333335,2.666666666666667,34.0 | ||
2020-03,8.333333333333332,3.571428571428571,23.214285714285715,3.571428571428571,5.952380952380952,20.238095238095237,0.0,10.714285714285714,2.380952380952381,22.023809523809522 | ||
2020-04,15.384615384615385,1.183431952662722,20.710059171597635,1.7751479289940828,10.059171597633137,20.118343195266274,0.0,5.325443786982249,2.366863905325444,23.076923076923077 | ||
2020-05,17.857142857142858,0.5952380952380952,13.690476190476192,1.7857142857142856,7.142857142857142,17.261904761904763,0.5952380952380952,4.166666666666666,0.5952380952380952,36.30952380952381 | ||
2020-06,13.414634146341465,5.487804878048781,15.853658536585366,9.146341463414634,3.048780487804878,13.414634146341465,0.6097560975609756,2.4390243902439024,1.2195121951219512,35.36585365853659 | ||
2020-07,6.206896551724138,2.7586206896551726,9.655172413793103,1.3793103448275863,10.344827586206897,22.06896551724138,0.0,11.03448275862069,0.0,36.55172413793103 | ||
2020-08,21.804511278195488,0.7518796992481203,12.030075187969924,0.7518796992481203,6.015037593984962,17.293233082706767,0.0,11.278195488721805,2.2556390977443606,27.819548872180448 | ||
2020-09,8.02919708029197,0.7299270072992701,4.37956204379562,0.7299270072992701,13.138686131386862,21.897810218978105,0.0,4.37956204379562,0.0,46.715328467153284 | ||
2020-10,2.083333333333333,3.125,32.29166666666667,0.0,15.625,5.208333333333334,1.0416666666666665,2.083333333333333,0.0,38.54166666666667 | ||
2020-11,8.653846153846153,0.0,37.5,0.0,0.9615384615384616,7.6923076923076925,0.0,5.769230769230769,0.0,39.42307692307692 | ||
2020-12,8.51063829787234,1.4184397163120568,34.751773049645394,2.127659574468085,2.8368794326241136,3.546099290780142,1.4184397163120568,1.4184397163120568,1.4184397163120568,42.5531914893617 | ||
2021-01,4.0,0.0,24.0,0.0,4.0,1.3333333333333335,0.0,0.0,1.3333333333333335,65.33333333333333 | ||
2021-02,5.607476635514018,5.607476635514018,38.31775700934579,0.0,9.345794392523365,0.9345794392523363,0.9345794392523363,0.9345794392523363,0.9345794392523363,37.38317757009346 | ||
2021-03,13.178294573643413,0.0,37.2093023255814,1.550387596899225,5.426356589147287,17.05426356589147,0.7751937984496124,1.550387596899225,2.3255813953488373,20.930232558139537 | ||
2021-04,6.730769230769231,0.0,7.6923076923076925,3.8461538461538463,13.461538461538462,25.961538461538463,0.0,2.8846153846153846,1.9230769230769231,37.5 | ||
2021-05,15.306122448979592,0.0,12.244897959183673,1.0204081632653061,1.0204081632653061,10.204081632653061,0.0,3.061224489795918,1.0204081632653061,56.12244897959183 | ||
2021-06,13.793103448275861,1.1494252873563218,29.88505747126437,0.0,5.747126436781609,3.4482758620689653,0.0,0.0,3.4482758620689653,42.5287356321839 | ||
2021-07,10.638297872340425,1.0638297872340425,10.638297872340425,2.127659574468085,11.702127659574469,8.51063829787234,0.0,2.127659574468085,2.127659574468085,51.06382978723404 | ||
2021-08,7.964601769911504,2.6548672566371683,16.8141592920354,1.7699115044247788,5.3097345132743365,10.619469026548673,0.0,3.5398230088495577,1.7699115044247788,49.557522123893804 | ||
2021-09,21.008403361344538,0.8403361344537815,16.80672268907563,1.680672268907563,15.966386554621847,5.88235294117647,0.0,3.361344537815126,1.680672268907563,32.773109243697476 | ||
2021-10,11.650485436893204,8.737864077669903,16.50485436893204,0.9708737864077669,12.62135922330097,0.9708737864077669,0.0,0.0,0.9708737864077669,47.57281553398058 | ||
2021-11,3.4482758620689653,0.8620689655172413,23.275862068965516,0.8620689655172413,1.7241379310344827,11.206896551724139,0.0,0.8620689655172413,4.310344827586207,53.44827586206896 | ||
2021-12,1.0752688172043012,1.0752688172043012,31.182795698924732,1.0752688172043012,5.376344086021505,2.1505376344086025,0.0,4.301075268817205,1.0752688172043012,52.68817204301075 | ||
2022-01,13.829787234042554,4.25531914893617,26.595744680851062,3.1914893617021276,0.0,5.319148936170213,0.0,1.0638297872340425,1.0638297872340425,44.680851063829785 | ||
2022-02,15.18987341772152,0.0,12.658227848101266,1.2658227848101267,8.860759493670885,13.924050632911392,0.0,2.5316455696202533,1.2658227848101267,44.303797468354425 | ||
2022-03,20.98765432098765,6.172839506172839,22.22222222222222,0.0,3.7037037037037033,4.938271604938271,0.0,1.2345679012345678,2.4691358024691357,38.2716049382716 | ||
2022-04,4.615384615384616,7.6923076923076925,18.461538461538463,0.0,9.230769230769232,0.0,0.0,0.0,0.0,60.0 | ||
2022-05,20.253164556962027,3.79746835443038,15.18987341772152,0.0,5.063291139240507,3.79746835443038,0.0,29.11392405063291,0.0,22.78481012658228 | ||
2022-06,13.20754716981132,6.60377358490566,27.358490566037734,1.8867924528301887,0.9433962264150944,1.8867924528301887,0.0,21.69811320754717,0.0,26.41509433962264 | ||
2022-07,11.235955056179774,4.49438202247191,28.08988764044944,2.247191011235955,3.3707865168539324,8.98876404494382,1.1235955056179776,3.3707865168539324,1.1235955056179776,35.95505617977528 | ||
2022-08,27.184466019417474,1.9417475728155338,24.271844660194176,0.0,5.825242718446602,3.8834951456310676,0.9708737864077669,10.679611650485436,0.0,25.24271844660194 | ||
2022-09,30.275229357798167,0.0,22.018348623853214,0.0,12.844036697247708,4.587155963302752,0.0,10.091743119266056,0.0,20.18348623853211 | ||
2022-10,13.580246913580247,0.0,17.28395061728395,3.7037037037037033,13.580246913580247,1.2345679012345678,0.0,2.4691358024691357,1.2345679012345678,46.913580246913575 | ||
2022-11,11.235955056179774,3.3707865168539324,20.224719101123593,1.1235955056179776,3.3707865168539324,5.617977528089887,0.0,0.0,0.0,55.0561797752809 | ||
2022-12,6.779661016949152,6.779661016949152,16.94915254237288,1.694915254237288,11.864406779661017,1.694915254237288,0.0,3.389830508474576,0.0,50.847457627118644 | ||
2023-01,6.8181818181818175,0.0,28.40909090909091,0.0,4.545454545454546,4.545454545454546,0.0,0.0,3.4090909090909087,52.27272727272727 | ||
2023-02,16.071428571428573,17.857142857142858,14.285714285714285,0.0,7.142857142857142,5.357142857142857,0.0,0.0,0.0,39.285714285714285 | ||
2023-03,40.44943820224719,10.112359550561797,11.235955056179774,0.0,4.49438202247191,5.617977528089887,0.0,1.1235955056179776,2.247191011235955,24.719101123595504 | ||
2023-04,18.085106382978726,4.25531914893617,24.46808510638298,4.25531914893617,9.574468085106384,7.446808510638298,0.0,2.127659574468085,1.0638297872340425,28.723404255319153 | ||
2023-05,45.535714285714285,1.7857142857142856,16.964285714285715,0.8928571428571428,2.6785714285714284,6.25,0.8928571428571428,0.0,2.6785714285714284,22.321428571428573 | ||
2023-06,21.11111111111111,2.2222222222222223,22.22222222222222,0.0,6.666666666666667,10.0,0.0,3.3333333333333335,1.1111111111111112,33.33333333333333 | ||
2023-07,20.43010752688172,5.376344086021505,11.827956989247312,0.0,5.376344086021505,7.526881720430108,0.0,7.526881720430108,0.0,41.935483870967744 | ||
2023-08,26.0,5.0,17.0,0.0,4.0,10.0,0.0,9.0,0.0,28.999999999999996 | ||
2023-09,33.76623376623377,5.194805194805195,11.688311688311687,0.0,18.181818181818183,6.493506493506493,0.0,0.0,1.2987012987012987,23.376623376623375 | ||
2023-10,25.984251968503933,1.574803149606299,27.559055118110237,1.574803149606299,11.023622047244094,3.149606299212598,0.0,1.574803149606299,0.0,27.559055118110237 | ||
2023-11,8.064516129032258,5.64516129032258,30.64516129032258,0.8064516129032258,7.258064516129033,7.258064516129033,0.0,0.8064516129032258,0.8064516129032258,38.70967741935484 | ||
2023-12,11.842105263157894,7.894736842105263,17.105263157894736,2.631578947368421,10.526315789473683,3.9473684210526314,1.3157894736842104,1.3157894736842104,1.3157894736842104,42.10526315789473 | ||
2024-01,6.666666666666667,4.444444444444445,35.55555555555556,4.444444444444445,11.11111111111111,4.444444444444445,0.0,0.0,0.0,33.33333333333333 |