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 - HexBin Plot
Selected Reading
Python Pandas - HexBin PlotHexagonal bin plot, also known as hexbin plot, is an alternative to scatter plots, especially when the data is too dense to visualize individual points. Instead of plotting each data point, the hexagonal bin plot divides the plane into hexagons and aggregates data within each hexagon. The Pandas library provides the DataFrame.plot.hexbin() method for creating the hexagonal bin plot. In this tutorial, we will learn about how to use the Pandas DataFrame.plot.hexbin() method for creating and customizing hexagonal bin plot with different examples. Hexagonal Bin Plot in PandasBy using the DataFrame.plot.hexbin() method, you can create a hexagonal bin plot for x versus y data. Resulting a Matplotlib Axes on which the hexagonal bin plot is rendered. SyntaxFollowing is the syntax of the plot.hexbin() method − DataFrame.plot.hexbin(x, y, C=None, reduce_C_function=None, gridsize=None, **kwargs) Where,
ExampleLet's create a basic hexagonal bin plot using the DataFrame.plot.hexbin() method. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
df = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])
df["Col2"] = df["Col2"] + np.arange(500)
# Create a hexagonal bin plot
df.plot.hexbin(x="Col1", y="Col2", gridsize=25)
# Set title and Display the plot
plt.title('Basic Hexagonal Bin Plot')
plt.show()
OutputFollowing is the output of the above code −
Applying Aggregation to the Hexbin PlotYou can apply additional data aggregation function to reduce bin values into a single number using the C and reduce_C_function parameters. The parameter C specifies the values at each (x, y) points, and the reduce_C_function parameter takes a function to aggregate values in each bin to a single number. ExampleThe following example demonstrates creating a hexagonal bin plot with an additional aggregation function using the C and reduce_C_function parameters. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
df = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])
df["Col2"] = df["Col2"] + np.arange(500)
# Add a value column
df["Col3"] = np.random.uniform(0, 3, 500)
# Create a hexagonal bin plot with aggregation
df.plot.hexbin(x="Col1", y="Col2", C="Col3", reduce_C_function=np.max, gridsize=25)
# Set title and Display the plot
plt.title('Hexagonal Bin Plot with Aggregation')
plt.show()
OutputOn executing the above code we will get the following output −
Customizing Hexagonal Bin PlotsYou can enhance the visual appeal the hexagonal bin plots by customizing parameters like colormap, transparency, edgecolors, and more. ExampleThe following example demonstrates customizing the hexagonal bin plot using the colormap, transparency, and edgecolors parameters. import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
# Generate sample data
df = pd.DataFrame(np.random.randn(500, 2), columns=["Col1", "Col2"])
df["Col2"] = df["Col2"] + np.arange(500)
# Add a value column
df["Col3"] = np.random.uniform(0, 3, 500)
# Create a hexagonal bin plot with customizations
df.plot.hexbin(x="Col1", y="Col2", gridsize=25, cmap='magma', edgecolors="black", alpha=0.7)
# Set title and Display the plot
plt.title('Hexagonal Bin Plot with Customization')
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. |