# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

# Input data files are available in the read-only "../input/" directory
# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory

import os
for dirname, _, filenames in os.walk('/kaggle/input'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using "Save & Run All" 
# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import seaborn as sns 
import matplotlib.pyplot as plt
import geopandas as geo
import warnings
warnings.filterwarnings("ignore")
%matplotlib inline
df=pd.read_csv("../input/mineral-ores-around-the-world/Mineral ores round the world.csv")
df.head()
from shapely.geometry import Polygon,Point
import geoplot
world = geo.read_file(geo.datasets.get_path('naturalearth_lowres'))
world1=df["country"].value_counts().to_frame().reset_index()
world1["index"]=world1["index"].replace({"United States":'United States of America'})
world1=world1.rename(columns={"index":"name"})
world=world[["name","geometry"]].merge(world1,on="name")
import mapclassify
site_counts = world["country"]
scheme = mapclassify.MaximumBreaks(site_counts)

# Note: this code sample requires geoplot>=0.4.0.
geoplot.choropleth(
    world, hue=site_counts, scheme=scheme,
    cmap='Paired', figsize=(16, 8)
)
plt.show() #some datas are missing really sorry for that.
"""
<h1 style="text-align:left">USA,Mexico,Chile,Peru,Argentina has most number of sites.</h1>
"""
#Our key focus is India.
india_df=df[df["country"]=="India"]
india_df=india_df.reset_index().drop(columns=["index"])
india_df.head() # taking india's ores only.
india_df["state"]=india_df['state'].replace({'Orissa': 'Odisha', 'Tamil Nadu [Madras]': 'Tamil Nadu'})
shp_gdf = geo.read_file('../input/india-gis-data/India States/Indian_states.shp')
gdf = geo.GeoDataFrame(
    india_df.iloc[:,:5], geometry=geo.points_from_xy(india_df.iloc[:,:5].longitude, india_df.iloc[:,:5].latitude))
polygon = Polygon([(30, 0), (30, 90), (180, 90), (180, 0), (30,0)])
poly_gdf = geo.GeoDataFrame([1], geometry=[polygon], crs=world.crs)
fig, (ax1,ax2) = plt.subplots(1,2,figsize=(20, 10))
world.plot(ax=ax1)
poly_gdf.boundary.plot(ax=ax1, color="brown")
ax1.set_title("Asia in World Map", fontsize=20)
ax1.set_axis_off()

world.clip(polygon).plot(ax=ax2, color="lightpink")
world.boundary.plot(ax=ax2)
shp_gdf.boundary.plot(ax=ax2, color="brown")

ax2.set_title("Asia in World(Asia filled in color)", fontsize=20)
ax2.set_axis_off()
plt.show()

world = geo.read_file(geo.datasets.get_path('naturalearth_lowres'))

# Asia.
ax = world[world.continent == 'Asia'].plot(
    color='white', edgecolor='black')

# We can now plot our ``GeoDataFrame``.
gdf.plot(ax=ax, color='brown')

plt.title("India's mineral ores")

plt.show()
capitals = geo.read_file(geo.datasets.get_path("naturalearth_cities"))#capitals in World
clip_caps = capitals.clip(world[world["continent"] == "Asia"]) #capitals in Asia 

# Plot the clipped data
# The plot below shows the results of the clip function applied to the capital cities
fig, ax = plt.subplots(figsize=(12, 8))
clip_caps.plot(ax=ax, color="green")
gdf[["site_name","geometry"]].plot(ax=ax,color="brown")
world[world["continent"] == "Asia"].boundary.plot(ax=ax, color="grey")
ax.set_title("Capitals of each country in Asia \n away from indian mineral ores ", fontsize=20)
ax.set_axis_off()
ax.legend(["capital city","India's ore present"],loc="lower left",title="Legends")
plt.show()
indian_1=india_df["state"].value_counts().to_frame()
indian_1=indian_1.reset_index().rename(columns={"index":"state","state":"mineral_ore_present"})

merged = shp_gdf.set_index('st_nm').join(indian_1.set_index('state')).fillna(0)
fig, ax = plt.subplots(1, figsize=(12, 12))
ax.axis('off')
ax.set_title('Mineral ores in India (state wise)',
             fontdict={'fontsize': '15', 'fontweight' : '3'})
fig = merged.plot(column='mineral_ore_present', cmap='Accent', linewidth=0.5, ax=ax, edgecolor='0.2',legend=True)
import folium
ores_name=india_df.iloc[:,:3].drop_duplicates(subset=["site_name"])
title_html = '''
             <h3 align="left" style="font-size:16px"><b>{}</b></h3>
             '''.format("India's mineral ore(by name)")   
merge_df=ores_name.merge(india_df["site_name"].value_counts().to_frame().reset_index().rename(columns={"site_name":"site_present","index":"site_name"}),on="site_name");
n = folium.Map(location=[22,80], tiles="OpenStreetMap", zoom_start=4,width=1000,height=800)
for i in range(0,len(merge_df)):
    folium.CircleMarker(
        location=[merge_df.iloc[i]['latitude'], merge_df.iloc[i]['longitude']],
        popup=merge_df.iloc[i]['site_name'],tooltip=merge_df.iloc[i]['site_name'],
        radius=float(merge_df.iloc[i]['site_present'])*1,
        color='#69b3a2',
        fill=True,
        fill_color='brown',
    ).add_to(n)
n.get_root().html.add_child(folium.Element(title_html))
n
"""
<h1 style ="text-align:center"><u>Minerals present in State</u></h1>
"""
india_=india_df.loc[:,["state","commod1","commod2","commod3"]]
melted_df=pd.melt(india_, id_vars =['state'], value_vars =['commod1',"commod2","commod3"])
new_df=melted_df.drop_duplicates().dropna()[["state","value"]];
new_df1=new_df.set_index(["state"]).sort_index()
import re
new_df1["Minerals_present"]=[re.split(r"[-;,.\s]\s*", i) for i in new_df1["value"]]
new_df2=new_df1.reset_index().groupby("state")["Minerals_present"].sum()
new_df3=new_df2.to_frame().reset_index()
new_df3["Minerals_present"]=[list(set(i)) for i in new_df3["Minerals_present"]]
new_df3["Count_of_minerals"]=[len(i) for i in new_df3["Minerals_present"]]
from IPython.display import HTML
HTML(new_df3.loc[:,["state","Minerals_present"]].to_html(classes='table table-striped'))
#function to return the value with its percent
def make_autopct(values):
    def my_autopct(pct):
        total = sum(values)
        val = int(round(pct*total/100.0))
        return '{p:.2f}%  ({v:d})'.format(p=pct,v=val)
    return my_autopct
import matplotlib
colors=[k for k,v in matplotlib.colors.cnames.items()][30:48]
new_geolist=india_df.dep_type.unique().tolist()
new_geo=india_df[["site_name","latitude","longitude","dep_type"]].dropna().set_index("dep_type").reset_index() #deposit type.
# creating a geometry column 
geometry = [Point(xy) for xy in zip(new_geo['longitude'], new_geo['latitude'])]
# Coordinate reference system : WGS84
crs = {'init': 'epsg:4326'}
# Creating a Geographic data frame 
gdf = geo.GeoDataFrame(new_geo, crs=crs, geometry=geometry)
values=india_df["oper_type"].value_counts().values
fig3 = plt.figure(constrained_layout=True,figsize=(20,15))
gs = fig3.add_gridspec(3, 3)
f3_ax1 = fig3.add_subplot(gs[0, :])
sns.barplot(x="state",y="Count_of_minerals",data=new_df3,ax=f3_ax1)
f3_ax1.tick_params(labelrotation=45);f3_ax1.set_title("Counts of Minerals present in states")
f3_ax2 = fig3.add_subplot(gs[1, :-1])
f3_ax2.set_title('Where is Mineral Present',)
india_df["oper_type"].value_counts().plot.pie(ax=f3_ax2,autopct=make_autopct(values))
f3_ax3 = fig3.add_subplot(gs[1:, -1])
f3_ax3.set_title('Map of Different deposit type(excluding not available)')
shp_gdf.plot(ax=f3_ax3,color="grey",alpha=0.45)
for i,j in zip(new_geolist,colors):
    gdf[gdf["dep_type"]==i].plot(ax=f3_ax3,color=j,marker="o",label=i)
f3_ax4 = fig3.add_subplot(gs[-1, 0])
f3_ax4.set_title('Composition type or Formation')
plt.barh(india_df["com_type"].value_counts().keys(), india_df["com_type"].value_counts().values,color=["lightpink","lightgreen","skyblue"])
for index, value in enumerate(india_df["com_type"].value_counts().values):
    plt.text(value, index,
             str(value))
f3_ax5 = fig3.add_subplot(gs[-1, -2])
f3_ax5.set_title('Different Product Size')
india_df.prod_size.value_counts().plot.barh(color=[k for k,v in matplotlib.colors.cnames.items()][30:36],ax=f3_ax5)
f3_ax5.yaxis.tick_right()
plt.suptitle("Various graphs in One",fontsize=30,x=0.01)
plt.show()
fig, ax = plt.subplots(figsize=(20, 12))
shp_gdf.plot(color="grey",alpha=0.35,ax=ax)
for i,j in zip(new_geolist,colors):
    gdf[gdf["dep_type"]==i].plot(color=j,marker="o",label=i,ax=ax)
ax.set_axis_off()
plt.title(" Different Deposits in India(Excluding Unknown or NaN) ")
plt.legend(prop={"size":7},loc="upper right")
plt.show()
o_gag=india_df[["state","gangue","ore"]]
o_gag=o_gag.groupby("state")["gangue","ore"].sum().reset_index().merge(new_df3,on="state")
o_gag1=o_gag.loc[:,["gangue","ore","Minerals_present"]]
o_gag1=o_gag1[o_gag1["ore"]!=0]
from PIL import Image
from io import BytesIO 
from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
stopwords = set(STOPWORDS)
wordcloud = WordCloud(stopwords=stopwords,max_words=150,background_color="white",collocations=False,mode="RGBA").generate(' '.join(o_gag1["ore"])).to_array()
plt.figure(figsize=(15,10))
plt.imshow(wordcloud, interpolation= 'sinc')
plt.axis("off")
plt.title("Ores Present In India",fontsize=20)
plt.show() #Ores are the those from which we get minerals.
import requests
empty_list=[]
new_list=o_gag1["gangue"].drop_duplicates().tolist()
new_list.pop(1)
new_sum=[re.split(r"[-;,.\s]\s*", i) for i in new_list]
for elem1 in new_sum:
    for elem2 in elem1:
        empty_list.append(elem2)

stopwords = set(STOPWORDS)
response = requests.get("https://mpng.subpng.com/20180928/yt/kisspng-vector-graphics-states-of-india-map-clip-art-national-labour-market-information-system-nation-5baebd72aeca78.270318021538178418716.jpg")
img = Image.open(BytesIO(response.content))
mask = np.array(img)
wordcloud = WordCloud(stopwords=stopwords,max_words=50,background_color="white",mask=mask,repeat="None",collocations=False).generate(' '.join(empty_list)).to_array()
plt.figure(figsize=(15,10))
plt.imshow(wordcloud, interpolation= 'sinc')
plt.axis("off")
plt.title("Gangues Present In Indian ores",fontsize=20)
plt.show()#change image as you like image as mask is Indian map.
!pip install upsetplot
from upsetplot import UpSet
india_new=india_df.loc[:,["dev_stat","state"]].dropna(subset=["state"]).fillna("Unknown")
india_new=india_new.set_index("state")
india_new=pd.get_dummies(india_new).reset_index().set_index(["dev_stat_Occurrence","dev_stat_Past Producer",
                                                             "dev_stat_Plant","dev_stat_Producer","dev_stat_Prospect","dev_stat_Unknown"])
upset = UpSet(india_new,
              intersection_plot_elements=0, orientation="vertical",facecolor="black")  # disable the default bar chart
upset.add_stacked_bars(by="state", colors=colors,
                       title="Count by states",elements=50)
upset.plot()
fig.legend(fontsize=6, bbox_to_anchor=(0.5, 0.5, 0.1, 0.4),ncol=5)
plt.suptitle("Development status in each state",fontsize=15)
plt.show()
other_df=pd.read_csv("../input/poptable/poptable.csv")[["State.Name","latitude","longitude"]]
other_df=other_df.set_axis(['state', "latitude","longitude"], axis=1, inplace=False)
listq=[i.rstrip() for i in other_df["state"]]
other_df["state"] = listq
other_df.state=other_df.state.replace({"ORISSA":'ODISHA'})
df2=india_df.loc[:,["state","work_type"]].fillna("Unknown")
example=df2.groupby(["state","work_type"])["work_type"].count()
example1=example.to_frame()
example1=example1.rename(columns={"work_type":"count_of_work"}).reset_index()
example1.state=[i.upper() for i in example1.state.tolist()]
new_merged=example1.merge(other_df,on="state")
temp = []
for count, item in enumerate(new_merged.iterrows()):
    # if count in comp_rows:
    temp.append( list(item[1]))
    
temp=[['ANDHRA PRADESH','Underground',0,14.750429,78.570026],
 ["ASSAM","Surface",0,26.749981,94.216667],
 ["ASSAM","Underground",0,26.749981,94.216667],
 ["GOA","Surface",0,15.491997,73.818001],
 ["GOA","Underground",0,15.491997,73.818001],
 ["HIMACHAL PRADESH","Surface",0,31.100025,77.166597],
 ["HIMACHAL PRADESH","Underground",0,31.100025,77.166597],
 ["JAMMU AND KASHMIR","Surface",0,34.299959,74.466658],
 ["JAMMU AND KASHMIR","Underground",0,34.299959,74.466658],
 ["KERALA","Underground",0,8.900373,76.569993],
 ["MAHARASHTRA","Surface",0,19.250232,73.160175],
 ["MANIPUR","Underground",0,24.799971,93.950017],
 ["MANIPUR","Surface",0,24.799971,93.950017],
 ["NAGALAND","Underground",0,25.666998,94.11657],
 ["NAGALAND","Surface",0,25.666998,94.11657],
 ["ODISHA","Underground",0,19.82043,85.900017],
 ["PUNJAB","Surface",0,31.519974,75.980003],
 ["PUNJAB","Underground",0,31.519974,75.980003],
 ["TAMIL NADU","Surface",0,12.920386,79.150042],
 ["TAMIL NADU","Underground",0,12.920386,79.150042],
 ["WEST BENGAL","Surface",0,22.58039,88.329947],
 ["WEST BENGAL","Underground",0,22.58039,88.329947],
 ["UTTAR PRADESH","Surface",0,27.599981,78.050006]
]

new_df_merged=new_merged.append(pd.DataFrame(temp, columns=["state","work_type","count_of_work","latitude","longitude"]))
new_df_merged.reset_index().drop(columns=["index"])
new_df_merged["geometry"] = [Point(xy) for xy in zip(new_df_merged['longitude'], new_df_merged['latitude'])]
gdf1 = geo.GeoDataFrame(new_df_merged, crs=crs, geometry=new_df_merged["geometry"]);gdf1
from plotnine import *
import plotnine as p9

(ggplot(gdf1, aes('state', 'work_type', fill='count_of_work'))
 + geom_tile(aes(width=.95, height=.95))
 +  scale_fill_gradient2(low = "#075AFF",
                       mid = "#FFFFCC",
                       high = "#FF0000") +
  coord_fixed()
 + geom_text(aes(label='count_of_work'), size=12,color="black")
 + labs(title="Sites available in states(as per there work requirement) ",fontweight='bold', size=10, color="Black")
 + theme(figure_size=(20, 10), text=element_text(size=14))
 + theme(axis_text_x=element_text(rotation=90, hjust=1))
)     
new_df_merged["to_fit_inmap"]=new_df_merged["count_of_work"]/50;new_df_merged.sort_values(by=["state","work_type"]).reset_index().drop(columns=["index"])
fig = plt.figure(figsize=(20,15))
ax_map = fig.add_axes([0, 0, 1, 1])
shp_gdf.plot(ax=ax_map)
ax_bar1 = fig.add_axes([0.5*(1+-45/180) , 0.5*(1+50/90) , 0.05, 0.05])
ax_bar1.bar([1, 2, 3], [0.14,0,0], color=[ 'C3','C1','C2'])
ax_bar1.set_axis_off()
ax_bar1.legend(["Unknown","Surface","Underground"], borderpad=2)
ax_bar = fig.add_axes([0.5*(1+-90/180) , 0.5*(1+-10/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.36], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+20/180) , 0.5*(1+10/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.04,0.06,1.58], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+50/180) , 0.5*(1+25/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.04], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-65/180) , 0.5*(1+-50/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.04,0.02,1.08], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-20/180) , 0.5*(1+-30/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.08,0,1.28], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+30/180) , 0.5*(1+0/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.20], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-35/180) , 0.5*(1+20/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0.02,0.06], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-40/180) , 0.5*(1+-60/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0.02,0.14], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+5/180) , 0.5*(1+-15/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.16,0,3.58], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-70/180) , 0.5*(1+50/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.12], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-80/180) , 0.5*(1+20/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.04,0.06,0.98], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+80/180) , 0.5*(1+25/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.02], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+70/180) , 0.5*(1+15/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.06], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar2 = fig.add_axes([0.5*(1+-70/180) , 0.5*(1+-20/90) , 0.05, 0.05])
ax_bar2.bar([1, 2, 3], [0.08,0.80,0], color=['C2', 'C3' ,'C1'])
ax_bar2.legend(["Underground","Unknown","Surface"], borderpad=2)
ax_bar2.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-50/180) , 0.5*(1+0/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.08,0.04,1.90], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-65/180) , 0.5*(1+-70/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.06,0,1.28], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-70/180) , 0.5*(1+70/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0,0,0.38], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
ax_bar = fig.add_axes([0.5*(1+-65/180) , 0.5*(1+-70/90) , 0.05, 0.05])
ax_bar.bar([1, 2, 3], [0.06,0,.28], color=['C1', 'C2', 'C3'])
ax_bar.set_axis_off()
plt.suptitle("Barplot for Worktype on Map")
ax_bar.legend(["Surface","Underground","Unknown"], borderpad=2)
ax_map.set_axis_off()
plt.show()