Using gridSpec to arrange multiple plots as Infographics using Python

[182 views]



Matplotlib library with all its flair, has a specific function named GridSpec(). The basic purpose of this function is to set the geometry of the subplots in a figure. But this function with a bit tweak can be used to present multiple plots into a fig, and covert it into a infographic to demonstrate a story with a minimal amount of coding.

No additional library is required to install for using GridSpec(), it works very efficiently, in case we have the base matplotlib installed with python.

Code:

import matplotlib.pyplot as plt from matplotlib.gridspec import GridSpec data_all_emission = pd.read_csv("annualemission.csv") # considering only last ten records for better demonstration data_all_emission = data_all_emission.tail(10) # Create a 2x2 grid of plots fig, axes = plt.subplots(3, 2, constrained_layout=True, figsize=(20,10)) gs = GridSpec(3, 3, figure=fig) # Plot 1 - Cummulative graph containing all the information - multiple bars axes[0,0].set_title("Year Wise Emission", color="b") data_all_emission.plot(ax=axes[0,0], x="year", y=["nh3", "nox", "so2", "voc", "pm10", "pm25"], kind="bar") # Plot 2 - Cummulative graph containing all the information - stacked bar axes[0,1].set_title("Year Wise Emission", color="b") ax = axes[0,1] data_all_emission.plot(x="year", y="nox", kind="bar", ax=ax) data_all_emission.plot(x="year", y="nh3", kind="bar", ax=ax, color="C6") data_all_emission.plot(x="year", y="so2", kind="bar", ax=ax, color="C2") data_all_emission.plot(x="year", y="voc", kind="bar", ax=ax, color="C3") data_all_emission.plot(x="year", y="pm10", kind="bar", ax=ax, color="C4") data_all_emission.plot(x="year", y="pm25", kind="bar", ax=ax, color="C5") # Plot 3 - Cummulative graph containing all the information - multiple column with line axes[1,0].set_title("", color="b") data_all_emission['pm10'].plot(ax = axes[1,0],kind='bar', color='red') data_all_emission['pm25'].plot(ax = axes[1,0],kind='line', marker='*', color='black', ms=10) # Plot 4 - Pie chart depicting the various emissions contribution for the last year axes[1,1].set_title("", color="b") ax = axes[1,1] ax.axis('equal') data_oneyear_emission = [87.75399456, 28.73753448, 2.500908237, 33.64595918, 24.68887142, 19.91823327] ax.pie(data_oneyear_emission, labels = ["nh3", "nox", "so2", "voc", "pm10", "pm25"],autopct='%1.2f%%') # Plot 5 - Line chart depicting the various emissions contribution axes[2,0].set_title("", color="b") axes[2,0].scatter(data_all_emission["year"], data_all_emission["pm10"], c ="pink",linewidths = 2,marker ="s",edgecolor ="green",s = 50) axes[2,0].scatter(data_all_emission["year"], data_all_emission["pm25"], c ="yellow",linewidths = 2,marker ="^",edgecolor ="red",s = 200) # Plot 6 - Scatter chart depicting various emission axes[2,1].set_title("", color="b") axes[2,1].plot(data_all_emission["year"], data_all_emission["nox"], label = "Nox", linestyle="-") axes[2,1].plot(data_all_emission["year"], data_all_emission["so2"], label = "So2", linestyle="--") axes[2,1].plot(data_all_emission["year"], data_all_emission["pm10"], label = "PM10", linestyle="-.") axes[2,1].plot(data_all_emission["year"], data_all_emission["pm25"], label = "PM25", linestyle=":") # depict illustration fig.suptitle("Emissions in UK for the last 10 years") plt.show()

annualemission.csv file:

year nh3 nox so2 voc pm10 pm25
1970 100 100 100 100 100
1971 99.60866 94.73848 100.2234 91.98165 88.64904
1972 99.24757 90.90876 97.98916 81.36547 77.83722
1973 105.1348 93.41885 103.5091 84.82977 79.18828
1974 98.61177 85.60625 99.59469 77.11811 73.4659
1975 95.78024 81.31908 97.78028 73.09667 64.93709
1976 97.4164 78.62831 100.1002 71.41608 63.7089
1977 97.98779 78.52168 103.2155 70.58038 63.24233
1978 99.4901 79.23909 104.9025 69.12694 60.81718
1979 103.0656 84.19333 107.6771 69.58273 62.19865
1980 100 98.25664 74.52168 104.6946 64.04883 55.17591
1981 99.64473 94.48094 67.67441 102.9414 61.43936 52.52082
1982 102.1541 94.39082 64.76253 103.6882 60.10658 51.66876
1983 102.4576 93.28414 59.77755 103.644 59.26957 49.91486
1984 103.4527 92.13409 57.51615 97.06149 52.983 45.03866
1985 102.5312 95.35628 57.84557 104.1628 58.49267 49.60396
1986 102.0716 98.30005 60.06391 108.1107 60.15754 51.62374
1987 103.205 100.8191 59.82018 109.9201 58.81281 47.46698
1988 101.1894 102.8586 58.89539 112.4591 57.67821 46.51254
1989 99.83748 104.6846 57.13024 114.2833 57.96487 45.92101
1990 102.5252 102.0491 54.90832 117.6629 55.32245 44.19974
1991 101.9632 99.60249 53.85406 115.0473 55.06404 44.22075
1992 98.03191 97.07307 52.44198 111.2425 51.90585 42.44986
1993 96.5465 91.67766 47.67251 105.5006 47.73274 39.24644
1994 98.11881 90.25446 43.36565 100.5698 45.08469 37.40968
1995 96.00009 86.45724 38.97931 93.29078 41.82557 34.35056
1996 98.99587 82.82792 33.17199 90.30169 41.66298 33.78143
1997 100.9373 76.19169 27.04854 86.20335 39.2198 32.1186
1998 101.2941 74.30948 26.90708 79.89956 37.16361 30.54426
1999 99.07644 70.2023 20.73919 72.12927 36.52284 30.23491
2000 96.96427 67.27746 19.83905 66.18495 34.57565 27.55024
2001 94.55035 66.14376 18.77597 63.42441 35.50784 27.43074
2002 93.54306 62.90013 16.90173 59.95043 31.20946 24.35101
2003 91.60464 61.86521 16.42902 55.57003 33.28321 24.5754
2004 91.5741 60.03181 13.93649 52.11064 30.90234 23.96429
2005 89.61648 59.29191 12.14972 48.79004 30.14321 23.66435
2006 88.22146 57.29529 11.44533 47.0125 29.24549 23.23405
2007 87.00332 54.92967 9.97534 45.38723 27.59104 22.04671

Output:

Using gridSpec to arrange multiple plots as Infographics using Python
        




Getting any Errors while coding? Ask us on our new Forum:




Liked Article? Please Buy Author a Coffee


Comments



Search
For Sponsored Advertisement OR Freelance Programming Article Submission, Contact us at atechdailyweb@gmail.com
Recommended Deal Ends in











Quizzes:
Online Games
Play 2048 Game Online and Relax.
Play 2048 Game Online

Why does everyone hate Java?
How much JavaScript to learn for Web Development
How to Convert EPOCH to Date in Java
Search Tags