Coordinate Systems¶
Coordinate systems control how x and y positions are mapped to the plot.
In [1]:
Copied!
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.rand(100) * 100,
'y': np.random.rand(100) * 100,
'category': np.random.choice(['A', 'B', 'C', 'D'], 100)
})
# Bar chart data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'value': [25, 40, 35, 30]
})
# Distribution data
dist_df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.concatenate([
np.random.normal(0, 1, 50),
np.random.normal(2, 1.5, 50),
np.random.normal(-1, 0.8, 50)
])
})
import pandas as pd
import numpy as np
from ggplotly import *
# Sample data
np.random.seed(42)
df = pd.DataFrame({
'x': np.random.rand(100) * 100,
'y': np.random.rand(100) * 100,
'category': np.random.choice(['A', 'B', 'C', 'D'], 100)
})
# Bar chart data
bar_df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'value': [25, 40, 35, 30]
})
# Distribution data
dist_df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.concatenate([
np.random.normal(0, 1, 50),
np.random.normal(2, 1.5, 50),
np.random.normal(-1, 0.8, 50)
])
})
coord_cartesian¶
The default Cartesian coordinate system. Use it to zoom without clipping data.
In [2]:
Copied!
# Zoom to a region (data outside is still used for calculations)
ggplot(df, aes(x='x', y='y')) + geom_point() + coord_cartesian(xlim=(20, 80), ylim=(20, 80))
# Zoom to a region (data outside is still used for calculations)
ggplot(df, aes(x='x', y='y')) + geom_point() + coord_cartesian(xlim=(20, 80), ylim=(20, 80))
Out[2]:
xlim/ylim vs coord_cartesian: xlim() and ylim() filter the data before plotting. coord_cartesian() zooms the view without removing data points. This matters for statistical calculations like geom_smooth.
coord_fixed¶
Fixed aspect ratio coordinates. Essential for maps and any plot where x and y must have equal scaling.
In [3]:
Copied!
# Square data should appear as a square with ratio=1
square_df = pd.DataFrame({'x': [0, 1, 1, 0, 0], 'y': [0, 0, 1, 1, 0]})
ggplot(square_df, aes(x='x', y='y')) + geom_path(size=2) + geom_point(size=10) + coord_fixed()
# Square data should appear as a square with ratio=1
square_df = pd.DataFrame({'x': [0, 1, 1, 0, 0], 'y': [0, 0, 1, 1, 0]})
ggplot(square_df, aes(x='x', y='y')) + geom_path(size=2) + geom_point(size=10) + coord_fixed()
Out[3]:
In [4]:
Copied!
# With ratio=2, the square appears as a tall rectangle (y is stretched)
ggplot(square_df, aes(x='x', y='y')) + geom_path(size=2) + geom_point(size=10) + coord_fixed(ratio=2)
# With ratio=2, the square appears as a tall rectangle (y is stretched)
ggplot(square_df, aes(x='x', y='y')) + geom_path(size=2) + geom_point(size=10) + coord_fixed(ratio=2)
Out[4]:
coord_flip¶
Flip x and y axes. Useful for horizontal bar charts and boxplots.
In [5]:
Copied!
# Horizontal bar chart
ggplot(bar_df, aes(x='category', y='value')) + geom_bar(stat='identity') + coord_flip()
# Horizontal bar chart
ggplot(bar_df, aes(x='category', y='value')) + geom_bar(stat='identity') + coord_flip()
Out[5]:
In [6]:
Copied!
# Horizontal boxplot
ggplot(dist_df, aes(x='category', y='value')) + geom_boxplot() + coord_flip()
# Horizontal boxplot
ggplot(dist_df, aes(x='category', y='value')) + geom_boxplot() + coord_flip()
Out[6]:
coord_polar¶
Polar coordinates for pie charts and radial plots.
coord_polar Parameters¶
coord_polar(
theta='x', # Variable mapped to angle ('x' or 'y')
start=0, # Starting angle in radians
direction=1 # 1 = clockwise, -1 = counter-clockwise
)
coord_sf¶
For geographic/spatial data with proper map projections. Requires geopandas.
import geopandas as gpd
# Load geographic data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Plot with projection
ggplot(world) + geom_sf() + coord_sf(crs='EPSG:4326')
Available Projections¶
'mercator'- Web Mercator (Google Maps style)'albers usa'- Albers USA (good for US maps)'orthographic'- Globe view'natural earth'- Natural Earth projection'robinson'- Robinson projection
Coordinate Reference¶
| Function | Description |
|---|---|
coord_cartesian |
Default Cartesian, zoom without clipping |
coord_fixed |
Fixed aspect ratio (ratio=1 for equal scaling) |
coord_flip |
Flip x and y axes |
coord_polar |
Polar coordinates |
coord_sf |
Geographic projections |
coord_polar Parameters¶
coord_polar(
theta='x', # Variable mapped to angle ('x' or 'y')
start=0, # Starting angle in radians
direction=1 # 1 = clockwise, -1 = counter-clockwise
)
coord_sf¶
For geographic/spatial data with proper map projections. Requires geopandas.
import geopandas as gpd
# Load geographic data
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
# Plot with projection
ggplot(world) + geom_sf() + coord_sf(crs='EPSG:4326')
Available Projections¶
'mercator'- Web Mercator (Google Maps style)'albers usa'- Albers USA (good for US maps)'orthographic'- Globe view'natural earth'- Natural Earth projection'robinson'- Robinson projection
Coordinate Reference¶
| Function | Description |
|---|---|
coord_cartesian |
Default Cartesian, zoom without clipping |
coord_flip |
Flip x and y axes |
coord_polar |
Polar coordinates |
coord_sf |
Geographic projections |