Matplotlib - Multi-Column vs. Stacked Column

[1278 views]




While preparing charts having requirement of plotting multiple columns against Y axis, using matplotlib in python, it is always difficult to plot stacked column which represents data in better format. Hence, developers are some time bound to use multi-columnar plot instead.

Matplotlib provide provisions for multiple types of plots, multi-columnar charts and stacked column charts are also part of it. However, stacked column is bit difficult to plot in comparison to multi-columnar chart. Apparently, stacked column chart represents the data more efficiently than multi-columnar chart.

With some very specific modifications in multi-columnar plot code, we can convert the chart to stacked column. Check out how.

For multi-columnar chart:

import pandas as pd data_all_emission = pd.read_csv("annualemission.csv") data_all_emission.plot(x="year", y=["nh3", "nox", "so2", "voc", "pm10", "pm25"], kind="bar")

For Stacked-column chart:

import pandas as pd data_all_emission = pd.read_csv("annualemission.csv") ax = data_all_emission.plot(x="year", y="nox", kind="bar", figsize=(20, 10)) 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") 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
                 





Comments





Search
Get Answers to your Programming Questions


Recommended Deals ends in








Quiz For You:

Search Tags