GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Python Pandas - Area Plot
Selected Reading
Python Pandas - Area PlotAn Area Plot, also known as an area chart or area graph, is a visualization tool that graphically represents quantitative data. It is an extension of a line chart where the area between the line and the axis is filled with colors, textures, or patterns. Area plots are commonly used to compare two or more quantities visually. Pandas provides an easy way to create stacked and unstacked area plots using the area() method. In this tutorial, we will learn about how to create and customize area plots using the Pandas library, with multiple examples demonstrating different plotting options. Area Plot in PandasIn Pandas, area plots can be created using the area() method for both the Series and DataFrames objects. This method resulting an area plot matplotlib.axes.Axes or an array numpy.ndarray of plots if subplots are enabled. It wraps the Matplotlib area() function. SyntaxFollowing is the syntax of the area() method − DataFrame.plot.area(x=None, y=None, stacked=True, **kwargs) Where,
Stacked Area PlotIn Pandas Area plots are stacked by default. To produce stacked area plot correctly, all column values must either be entirely positive or entirely negative. ExampleThis example creates a stacked area plot with the sum of all columns plotted at each x-coordinate using the DataFrame.plot.area() method. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7, 4]
# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])
# Generate a stacked area plot
df.plot.area()
plt.title("Stacked Area Plot")
plt.show()
OutputFollowing is the output of the above code −
Unstacked Area PlotTo create an unstacked area plot, set the stacked parameter to False. Which is useful for comparing the columns independently. ExampleThis example plots the unstacked area plot by using the DataFrame.plot.area() method. Here, the alpha parameter is used to adjust the transparency of the areas. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7, 4]
# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])
# Generate an unstacked area plot
df.plot.area(stacked=False, alpha=0.5)
plt.title("Unstacked Area Plot")
plt.show()
OutputOn executing the above code we will get the following output −
Handling Missing DataIf your data contains NaN values, Pandas will automatically fill these with 0. To handle missing data differently, use methods like fillna() or dropna() before plotting. ExampleThis example demonstrates handling the missing data before plotting the area plot. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 4), columns=["a", "b", "c", "d"])
# Add some NaN values
df.iloc[2:4, 1] = np.nan
# Fill NaN with a custom value
df.fillna(0).plot.area(stacked=True)
plt.title("Area Plot with NaN Values Filled")
plt.show()
OutputFollowing is the output of the above code −
Customizing Area PlotsYou can customize area plots using the various parameters available in Matplotlib, such as colors, colormap, labels, and gridlines. ExampleThis example customizes the area plot in Pandas using the additional keyword arguments. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7, 4]
# Create a DataFrame with random data
df = pd.DataFrame(np.random.rand(10, 3), columns=["a", "b", "c"])
# Customize area plot
df.plot.area(colormap="Greens", figsize=(10, 6))
plt.title("Customized Area Plot")
plt.xlabel("Index")
plt.ylabel("Values")
plt.show()
OutputAfter executing the above code, we get the following output −
python_pandas_visualization.htm Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |