Plotting Line Graph with Data Points in Pandas
Pandas is an open source data analysis library in Python. It provides many built-in methods to perform operations on numerical data.
Data visualization is very popular nowadays and is used to quickly analyze data visually. We can visualize the data in the Pandas data frame through a library called Matplotlib.
It provides a variety of charts to plot data.
Plotting a single line graph with data points in Pandas
A line graph is a type of graph that displays a series of data points called markers connected by straight lines. It represents the change in data points or trends over time.
In the following example, we have weather data. To plot the relationship between date and temperature, first, we have to list()
convert the required columns into a list using the method.
With plot()
the help of , we can draw a straight line graph by specifying x
and .y
We can further decorate the plot by specifying lines color
, linestyle
, marker
and . We also provide titles for both axes using the and methods.label
xlabel()
ylabel()
To rotate the x labels, we use xticks()
the method. For clarity, legend()
the line labels are displayed with colors in the corners of the chart.
Finally, we call show()
the method to display a line graph illustrating the relationship between date and temperature.
Sample code:
# Python 3.x
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("weather_data.csv")
display(df)
date = df["Date"]
temp = df["Temperature in Celcius"]
x = list(date)
y = list(temp)
plt.plot(x, y, color="g", linestyle="solid", marker="o", label="Temperature")
plt.xlabel("Date")
plt.xticks(rotation=25)
plt.ylabel("Temperature in Celcius")
plt.title("Temperature Data")
plt.legend()
plt.show()
Output:
Plotting Multiple Lines Using Data Points in Pandas
Suppose we want to visualize multiple attributes of a Pandas dataframe in a plot. In this case, we have to create many line plots, one for each line.
Each row has a different 颜色
sum 标签
. We show a line graph of temperature and humidity for each date in the following example.
Here, the legend helps distinguish the lines representing temperature and humidity.
Sample code:
# Python 3.x
import matplotlib.pyplot as plt
import pandas as pd
df = pd.read_csv("weather_data.csv")
display(df)
date = df["Date"]
temp = df["Temperature in Celcius"]
humidity = df["Humidity in %"]
x = list(date)
y1 = list(temp)
y2 = list(humidity)
plt.plot(x, y1, color="g", linestyle="solid", marker="o", label="Temperature")
plt.plot(x, y2, color="b", linestyle="solid", marker="o", label="Humidity")
plt.xlabel("Date")
plt.xticks(rotation=25)
plt.title("Temperature and Humidity Data")
plt.legend()
plt.show()
Output:
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
Finding the installed version of Pandas
Publish Date:2025/04/12 Views:190 Category:Python
-
Pandas is one of the commonly used Python libraries for data analysis, and Pandas versions need to be updated regularly. Therefore, other Pandas requirements are incompatible. Let's look at ways to determine the Pandas version and dependenc
KeyError in Pandas
Publish Date:2025/04/12 Views:81 Category:Python
-
This tutorial explores the concept of KeyError in Pandas. What is Pandas KeyError? While working with Pandas, analysts may encounter multiple errors thrown by the code interpreter. These errors are wide ranging and can help us better invest
Grouping and Sorting in Pandas
Publish Date:2025/04/12 Views:90 Category:Python
-
This tutorial explored the concept of grouping data in a DataFrame and sorting it in Pandas. Grouping and Sorting DataFrame in Pandas As we know, Pandas is an advanced data analysis tool or package extension in Python. Most of the companies
Converting Timedelta to Int in Pandas
Publish Date:2025/04/12 Views:123 Category:Python
-
This tutorial will discuss converting a to a using dt the attribute in Pandas . timedelta int Use the Pandas dt attribute to timedelta convert int To timedelta convert to an integer value, we can use the property pandas of the library dt .
Pandas fill NaN values
Publish Date:2025/04/12 Views:93 Category:Python
-
This tutorial explains how we can use DataFrame.fillna() the method to fill NaN values with specified values. We will use the following DataFrame in this article. import numpy as np import pandas as pd roll_no = [ 501 , 502 , 503 , 50
Pandas Convert String to Number
Publish Date:2025/04/12 Views:147 Category:Python
-
This tutorial explains how to pandas.to_numeric() convert string values of a Pandas DataFrame into numeric type using the method. import pandas as pd items_df = pd . DataFrame( { "Id" : [ 302 , 504 , 708 , 103 , 343 , 565 ], "Name" :
How to Change the Data Type of a Column in Pandas
Publish Date:2025/04/12 Views:139 Category:Python
-
We will look at methods for changing the data type of columns in a Pandas Dataframe, as well as options like to_numaric , , as_type and infer_objects . We will also discuss how to to_numaric use downcasting the option in . to_numeric Method
Get the first row of Dataframe Pandas
Publish Date:2025/04/12 Views:78 Category:Python
-
This tutorial explains how to use the get_first_row pandas.DataFrame.iloc attribute and pandas.DataFrame.head() get_first_row method from a Pandas DataFrame. We will use the following DataFrame in the following example to explain how to get
Pandas Drop Duplicate Rows in DataFrame
Publish Date:2025/04/12 Views:75 Category:Python
-
This tutorial explains how to DataFrame.drop_duplicates() remove all duplicate rows from a Pandas DataFrame using the remove_by method. DataFrame.drop_duplicates() grammar DataFrame . drop_duplicates(subset = None , keep = "first" , inplace