Introduction:
India is a diverse nation with 29 states and 8 Union Territories, each with its own unique geography and cultural identity. Visualizing these states on a map can be a powerful way to understand their distribution and relative sizes. In this tutorial, we’ll explore how to plot Indian states on the map of India using Python. By the end of this tutorial, you’ll be able to plot each state of India on map.
Understand the Indian States Plotter:
This project will help to remember 29 states of India as it is like a quiz of Indian states.
How will the python script work? First it will ask the user to input the state name (you can enter the state name in any letter case) and if the entered state is correct then it will plot the state name on map. It will also show the number of correct states out of 29 states you have plot on the map. You can write “exit” in the input to exit from the program and then it will return a file containing states that you need to remember.
Setting Up the Environment:
Before we delve into coding, make sure you have Python installed on your system. You can download the latest version from the official Python website and follow the installation instructions. Additionally, a code editor such as Visual Studio Code or PyCharm, will make coding and testing more convenient.
We’ll start by importing the turtle and pandas modules. We will use the turtle module for visuals or graphics and pandas module for reading or extracting data.
Python Script:
You will have to create three files named as “indian-map.gif“, “main.py“, and “indian-states.csv“. Download the image and copy-paste the code or content respectively. You can also download the GitHub repository zip file of this project.
# main.py
# importing modules
from turtle import Turtle, Screen
import pandas
# creating screen
screen = Screen()
screen.title("States of India Quiz")
screen.setup(width=630, height=737)
screen.bgpic("India-Map.gif")
# extracting data from "Indian-States.csv" file
data = pandas.read_csv("Indian-States.csv")
states_dict = {row.state:(row.x, row.y) for (index, row) in data.iterrows()}
# some variables to store values
add_guess = 0
answered_states = []
# keep running in the while loop until we input "EXIT" to exit
while True:
# plotting the states by taking the input
guessed_state = screen.textinput(f"{add_guess}/{len(states_dict)} states correct", "Enter a state name 👇").upper()
if guessed_state in states_dict:
answered_states.append(guessed_state)
add_guess += 1
tr = Turtle()
tr.penup()
tr.hideturtle()
tr.goto(states_dict[guessed_state])
tr.write(guessed_state, align="center", font=('Arial', 8, 'bold'))
tr.dot(5)
# creating a csv file of states that we need to learn while exiting
if guessed_state == "EXIT":
missing_states = [state for state in states_dict if state not in answered_states]
new_data = pandas.DataFrame(missing_states)
new_data.to_csv("states-to-learn.csv")
break
state,x,y
ANDHRA PRADESH, -78.0, -150.0
ARUNACHAL PRADESH, 226.0, 161.0
ASSAM, 199.0, 117.0
BIHAR, 61.0, 90.0
CHHATTISGARH, -18.0, -8.0
GOA, -177.0, -138.0
GUJARAT, -230.0, 37.0
HARYANA, -126.0, 168.0
HIMACHAL PRADESH, -109.0, 228.0
JAMMU & KASHMIR, -155.0, 270.0
JHARKHAND, 51.0, 41.0
KARNATAKA, -147.0, -152.0
KERALA, -130.0, -241.0
MADHYA PRADESH, -99.0, 37.0
MAHARASHTRA, -143.0, -46.0
MANIPUR, 230.0, 90.0
MEGHALAYA, 178.0, 93.0
MIZORAM, 211.0, 46.0
NAGALAND, 239.0, 113.0
ODISHA, 36.0, -19.0
PUNJAB, -145.0, 207.0
RAJASTHAN, -180.0, 117.0
SIKKIM, 118.0, 136.0
TAMIL NADU, -94.0, -250.0
TELANGANA, -76.0, -87.0
TRIPURA, 187.0, 58.0
LADAKH, -113.0, 292.0
UTTAR PRADESH, -42.0, 124.0
UTTARAKHAND, -67.0, 193.0
Output:
Conclusion!
Plotting Indian states on a map using Python is a valuable skill for data visualization and geographical analysis. With the turtle and pandas library, you can explore not only Indian states but also other regions around the world, making it a versatile tool for geographic data visualization. You can take this python project further by adding more features as your specific needs. Whether you’re a geography enthusiast or a coding enthusiast, this Python-powered state plotter project lets you embark on a journey of cartographic exploration Happy c!