Visualization and Prediction of Crop Production data using Python
Last Updated :
07 Oct, 2022
Prerequisite: Data Visualization in Python
Visualization is seeing the data along various dimensions. In python, we can visualize the data using various plots available in different modules.
In this article, we are going to visualize and predict the crop production data for different years using various illustrations and python libraries.
Dataset
The Dataset contains different crops and their production from the year 2013 – 2020.
Requirements
There are a lot of python libraries which could be used to build visualization like matplotlib, vispy, bokeh, seaborn, pygal, folium, plotly, cufflinks, and networkx. Of the many, matplotlib and seaborn seems to be very widely used for basic to intermediate level of visualizations.
However, two of the above are widely used for visualization i.e.
- Matplotlib: It is an amazing visualization library in Python for 2D plots of arrays, It is a multi-platform data visualization library built on NumPy arrays and designed to work with the broader SciPy stack. Use the below command to install this library:
pip install matplotlib
- Seaborn: This library sits on top of matplotlib. In a sense, it has some flavors of matplotlib while from the visualization point, it is much better than matplotlib and has added features as well. Use the below command to install this library:
pip install seaborn
Step-by-step Approach
- Import required modules
- Load the dataset.
- Display the data and constraints of the loaded dataset.
- Use different methods to visualize various illustrations from the data.
Visualizations
Below are some programs which indicates the data and illustrates various visualizations of that data:
Example 1:
Python3
# importing pandas module
import pandas as pd
# load the dataset
data = pd.read_csv('crop.csv')
# display top 5 values
data.head()
Output:

These are the top 5 rows of the dataset used.
Example 2:
Python3
# data description
data.info()
Output:

These are the data constraints of the dataset.
Example 3:
Python3
# 2011 crop data in histogram analysis
data['2011'].hist()
Output:

The above program depicts the crop production data in the year 2011 using histogram.
Example 4:
Python3
# 2012 crop data in histogram analysis
data['2012'].hist()
Output:

The above program depicts the crop production data in the year 2012 using histogram.
Example 4:
Python3
# 2013 crop data in histogram analysis
data['2013'].hist()
Output:

The above program depicts the crop production data in the year 2013 using histogram.
Example 5:
Python3
# display all year data
data.hist()
Output:

The above program depicts the crop production data of all the available time periods(year) using multiple histograms.
Example 6:
Python3
# import seaborn module
import seaborn as sns
# setting style
sns.set_style("whitegrid")
# plotting data using boxplot for 2013 - 2014
sns.boxplot(x='2013', y='2014', data=data)
Output:

Comparing crop productions in the year 2013 and 2014 using box plot.
Example 7:
Python3
# scatter plot 2013 data vs 2014 data
plt.scatter(data['2013'],data['2014'])
plt.show()
Output:

Comparing crop production in the year 2013 and 2014 using scatter plot.
Example 8:
Python3
# line plot 2013 data vs 2014 data
plt.plot(data['2013'],data['2014'])
plt.show()
Output:

Comparing crop productions in the year 2013 and 2014 using line plot.
Example 9:
Python3
# import required modules
import matplotlib.pyplot as plt
from scipy import stats
# assign data
x = data['2017']
y = data['2018']
# linear regression 2017 data vs 2018 data
slope, intercept, r, p, std_err = stats.linregress(x, y)
# function to return slope
def myfunc(x):
return slope * x + intercept
mymodel = list(map(myfunc, x))
# scatter
plt.scatter(x, y)
# plotting the data
plt.plot(x, mymodel)
# display the figure
plt.show()
Output:

Applying linear regression to visualize and compare predicted crop production data between the year 2017 and 2018.
Example 10:
Python3
# import required modules
import matplotlib.pyplot as plt
from scipy import stats
# assign data
x = data['2016']
y = data['2017']
# linear regression 2017 data vs 2018 data
slope, intercept, r, p, std_err = stats.linregress(x, y)
# function to return slope
def myfunc(x):
return slope * x + intercept
mymodel = list(map(myfunc, x))
# scatter
plt.scatter(x, y)
# plotting the data
plt.plot(x, mymodel)
# display the figure
plt.show()
Output:

Applying linear regression to visualize and compare predicted crop production data between the year 2016 and 2017.
Demo Video
This video shows how to depict the above data visualization and predict data, using Jupyter Notebook from scratch.
In this way various data visualizations and predictions can be computed.
Similar Reads
COVID-19 Data Visualization using matplotlib in Python It feels surreal to imagine how the virus began to spread from one person that is patient zero to four million today. It was possible because of the transport system. Earlier back in the days, we didnât have a fraction of the transportation system we have today. Well, what good practices you can fol
8 min read
Data Visualization using Matplotlib in Python Matplotlib is a widely-used Python library used for creating static, animated and interactive data visualizations. It is built on the top of NumPy and it can easily handles large datasets for creating various types of plots such as line charts, bar charts, scatter plots, etc. These visualizations he
10 min read
Data Visualisation in Python using Matplotlib and Seaborn It may sometimes seem easier to go through a set of data points and build insights from it but usually this process may not yield good results. There could be a lot of things left undiscovered as a result of this process. Additionally, most of the data sets used in real life are too big to do any an
14 min read
Plotly for Data Visualization in Python Plotly is an open-source Python library designed to create interactive, visually appealing charts and graphs. It helps users to explore data through features like zooming, additional details and clicking for deeper insights. It handles the interactivity with JavaScript behind the scenes so that we c
12 min read
Interactive visualization of data using Bokeh Bokeh is a Python library for creating interactive data visualizations in a web browser. It offers human-readable and fast presentation of data in an visually pleasing manner. If youâve worked with visualization in Python before, itâs likely that you have used matplotlib. But Bokeh differs from matp
4 min read
Top 8 Python Libraries for Data Visualization Data Visualization is an extremely important part of Data Analysis. After all, there is no better way to understand the hidden patterns and layers in the data than seeing them in a visual format! Donât trust me? Well, assume that you analyzed your company data and found out that a particular product
7 min read
Covid-19 Analysis and Visualization using Plotly Express In this article, we will discuss Analyse Covid-19 data and will visualize it using Plotly Express in Python. This article deals with creating dozens of bar charts, line graphs, bubble charts, scatter plots. The graph that will be made in this project will be of excellent quality. Envisioning COVID-1
12 min read
Introduction to VisPy: Mastering High-Performance 2D/3D Data Visualization in Python VisPy is a high-performance interactive 2D/3D data visualization library in Python. It offers an interface for high-quality visualization and manipulation of large data sets in 2D/3D. Leaning on the power of the GPU, it provides both efficient and interactive visualizations. VisPy leverages the comp
9 min read
Python Bokeh tutorial - Interactive Data Visualization with Bokeh Python Bokeh is a Data Visualization library that provides interactive charts and plots. Bokeh renders its plots using HTML and JavaScript that uses modern web browsers for presenting elegant, concise construction of novel graphics with high-level interactivity. Features of Bokeh: Flexibility: Boke
15+ min read
Top 10 Data Visualization Project Ideas in 2025 Nowadays excellent data visualization skills are high in demand in the IT industries. Data visualization refers to the graphical representation of information and data. It is the practice of translating information into a visual context such as a graph or map to make data easier for the human brain
6 min read