Basic Charts¶
Fundamental chart types for everyday data visualization.
In [1]:
Copied!
import pandas as pd
import numpy as np
from ggplotly import *
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 4, 3, 5, 4]
})
(ggplot(df, aes(x='x', y='y')) + geom_point())
import pandas as pd
import numpy as np
from ggplotly import *
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 4, 3, 5, 4]
})
(ggplot(df, aes(x='x', y='y')) + geom_point())
Out[1]:
Scatter with Color Mapping¶
In [2]:
Copied!
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100)
})
(ggplot(df, aes(x='x', y='y', color='category')) + geom_point())
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100)
})
(ggplot(df, aes(x='x', y='y', color='category')) + geom_point())
Out[2]:
Scatter with Size Mapping¶
In [3]:
Copied!
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'size_var': np.random.rand(100) * 50
})
(ggplot(df, aes(x='x', y='y', size='size_var')) + geom_point(color='steelblue', alpha=0.6))
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'size_var': np.random.rand(100) * 50
})
(ggplot(df, aes(x='x', y='y', size='size_var')) + geom_point(color='steelblue', alpha=0.6))
Out[3]:
Multiple Aesthetics¶
In [4]:
Copied!
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100),
'size_var': np.random.rand(100) * 50
})
(ggplot(df, aes(x='x', y='y', color='category', size='size_var')) + geom_point(alpha=0.7))
df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100),
'category': np.random.choice(['A', 'B', 'C'], 100),
'size_var': np.random.rand(100) * 50
})
(ggplot(df, aes(x='x', y='y', color='category', size='size_var')) + geom_point(alpha=0.7))
Out[4]:
Custom Point Styles¶
In [5]:
Copied!
(ggplot(df, aes(x='x', y='y')) + geom_point(size=10, color='red', shape='diamond'))
(ggplot(df, aes(x='x', y='y')) + geom_point(size=10, color='red', shape='diamond'))
Out[5]:
In [6]:
Copied!
x = np.linspace(0, 10, 100)
df = pd.DataFrame({'x': x, 'y': np.sin(x)})
(ggplot(df, aes(x='x', y='y')) + geom_line(color='steelblue', size=2))
x = np.linspace(0, 10, 100)
df = pd.DataFrame({'x': x, 'y': np.sin(x)})
(ggplot(df, aes(x='x', y='y')) + geom_line(color='steelblue', size=2))
Out[6]:
Multiple Lines¶
In [7]:
Copied!
df = pd.DataFrame({
'x': np.tile(np.linspace(0, 10, 50), 3),
'y': np.concatenate([
np.sin(np.linspace(0, 10, 50)),
np.cos(np.linspace(0, 10, 50)),
np.sin(np.linspace(0, 10, 50)) + np.cos(np.linspace(0, 10, 50))
]),
'group': np.repeat(['sin', 'cos', 'sin+cos'], 50)
})
(ggplot(df, aes(x='x', y='y', color='group')) + geom_line(size=2))
df = pd.DataFrame({
'x': np.tile(np.linspace(0, 10, 50), 3),
'y': np.concatenate([
np.sin(np.linspace(0, 10, 50)),
np.cos(np.linspace(0, 10, 50)),
np.sin(np.linspace(0, 10, 50)) + np.cos(np.linspace(0, 10, 50))
]),
'group': np.repeat(['sin', 'cos', 'sin+cos'], 50)
})
(ggplot(df, aes(x='x', y='y', color='group')) + geom_line(size=2))
Out[7]:
Line with Points¶
In [8]:
Copied!
df = pd.DataFrame({'x': range(1, 6), 'y': [2, 4, 3, 5, 4]})
(ggplot(df, aes(x='x', y='y')) + geom_line() + geom_point(size=10))
df = pd.DataFrame({'x': range(1, 6), 'y': [2, 4, 3, 5, 4]})
(ggplot(df, aes(x='x', y='y')) + geom_line() + geom_point(size=10))
Out[8]:
In [9]:
Copied!
import math
t_vals = [i * 4 * math.pi / 100 for i in range(100)]
spiral = pd.DataFrame({
'x': [t * math.cos(t) for t in t_vals],
'y': [t * math.sin(t) for t in t_vals],
})
(ggplot(spiral, aes(x='x', y='y'))
+ geom_path(color='steelblue', size=2)
+ labs(title='Spiral with geom_path'))
import math
t_vals = [i * 4 * math.pi / 100 for i in range(100)]
spiral = pd.DataFrame({
'x': [t * math.cos(t) for t in t_vals],
'y': [t * math.sin(t) for t in t_vals],
})
(ggplot(spiral, aes(x='x', y='y'))
+ geom_path(color='steelblue', size=2)
+ labs(title='Spiral with geom_path'))
Out[9]:
Star Shape¶
In [10]:
Copied!
points = 5
outer_r, inner_r = 1, 0.4
star_x, star_y = [], []
for i in range(points * 2 + 1):
angle = i * math.pi / points - math.pi / 2
r = outer_r if i % 2 == 0 else inner_r
star_x.append(r * math.cos(angle))
star_y.append(r * math.sin(angle))
star = pd.DataFrame({'x': star_x, 'y': star_y})
(ggplot(star, aes(x='x', y='y')) + geom_path(color='gold', size=3))
points = 5
outer_r, inner_r = 1, 0.4
star_x, star_y = [], []
for i in range(points * 2 + 1):
angle = i * math.pi / points - math.pi / 2
r = outer_r if i % 2 == 0 else inner_r
star_x.append(r * math.cos(angle))
star_y.append(r * math.sin(angle))
star = pd.DataFrame({'x': star_x, 'y': star_y})
(ggplot(star, aes(x='x', y='y')) + geom_path(color='gold', size=3))
Out[10]:
In [11]:
Copied!
df = pd.DataFrame({'category': np.random.choice(['A', 'B', 'C', 'D', 'E'], 200)})
(ggplot(df, aes(x='category')) + geom_bar())
df = pd.DataFrame({'category': np.random.choice(['A', 'B', 'C', 'D', 'E'], 200)})
(ggplot(df, aes(x='category')) + geom_bar())
Out[11]:
Colored by Category¶
In [12]:
Copied!
mpg = data('mpg')
(ggplot(mpg, aes(x='class', fill='class')) + geom_bar(alpha=0.8))
mpg = data('mpg')
(ggplot(mpg, aes(x='class', fill='class')) + geom_bar(alpha=0.8))
Out[12]:
Stacked Bar Chart¶
In [13]:
Copied!
(ggplot(mpg, aes(x='cyl', fill='drv')) + geom_bar())
(ggplot(mpg, aes(x='cyl', fill='drv')) + geom_bar())
Out[13]:
Dodged (Side-by-Side) Bar Chart¶
In [14]:
Copied!
(ggplot(mpg, aes(x='cyl', fill='drv')) + geom_bar(position='dodge'))
(ggplot(mpg, aes(x='cyl', fill='drv')) + geom_bar(position='dodge'))
Out[14]:
Custom Dodge Width¶
In [15]:
Copied!
(ggplot(mpg, aes(x='cyl', fill='drv'))
+ geom_bar(position=position_dodge(width=0.8)))
(ggplot(mpg, aes(x='cyl', fill='drv'))
+ geom_bar(position=position_dodge(width=0.8)))
Out[15]:
Column Charts¶
For pre-computed values, use geom_col:
In [16]:
Copied!
df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'value': [25, 40, 30, 55]
})
(ggplot(df, aes(x='category', y='value')) + geom_col(fill='steelblue'))
df = pd.DataFrame({
'category': ['A', 'B', 'C', 'D'],
'value': [25, 40, 30, 55]
})
(ggplot(df, aes(x='category', y='value')) + geom_col(fill='steelblue'))
Out[16]:
Grouped Column Chart¶
In [17]:
Copied!
df = pd.DataFrame({
'category': ['A', 'A', 'B', 'B', 'C', 'C'],
'group': ['G1', 'G2'] * 3,
'value': [10, 15, 20, 25, 15, 20]
})
(ggplot(df, aes(x='category', y='value', fill='group')) + geom_col(position='dodge'))
df = pd.DataFrame({
'category': ['A', 'A', 'B', 'B', 'C', 'C'],
'group': ['G1', 'G2'] * 3,
'value': [10, 15, 20, 25, 15, 20]
})
(ggplot(df, aes(x='category', y='value', fill='group')) + geom_col(position='dodge'))
Out[17]:
In [18]:
Copied!
df = pd.DataFrame({'x': np.random.randn(1000)})
(ggplot(df, aes(x='x')) + geom_histogram(fill='steelblue', alpha=0.7))
df = pd.DataFrame({'x': np.random.randn(1000)})
(ggplot(df, aes(x='x')) + geom_histogram(fill='steelblue', alpha=0.7))
Out[18]:
Custom Bins¶
In [19]:
Copied!
(ggplot(df, aes(x='x')) + geom_histogram(bins=30, color='white', fill='#FF6B6B'))
(ggplot(df, aes(x='x')) + geom_histogram(bins=30, color='white', fill='#FF6B6B'))
Out[19]:
Overlapping Histograms¶
In [20]:
Copied!
df = pd.DataFrame({
'x': np.concatenate([np.random.normal(0, 1, 500), np.random.normal(2, 1.5, 500)]),
'group': ['A'] * 500 + ['B'] * 500
})
(ggplot(df, aes(x='x', fill='group')) + geom_histogram(alpha=0.5, bins=30))
df = pd.DataFrame({
'x': np.concatenate([np.random.normal(0, 1, 500), np.random.normal(2, 1.5, 500)]),
'group': ['A'] * 500 + ['B'] * 500
})
(ggplot(df, aes(x='x', fill='group')) + geom_histogram(alpha=0.5, bins=30))
Out[20]:
In [21]:
Copied!
df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C', 'D'], 50),
'value': np.random.randn(200) * np.tile([1, 2, 1.5, 0.8], 50) + np.tile([0, 1, -1, 2], 50)
})
(ggplot(df, aes(x='category', y='value')) + geom_boxplot())
df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C', 'D'], 50),
'value': np.random.randn(200) * np.tile([1, 2, 1.5, 0.8], 50) + np.tile([0, 1, -1, 2], 50)
})
(ggplot(df, aes(x='category', y='value')) + geom_boxplot())
Out[21]:
Colored Boxplot¶
In [22]:
Copied!
(ggplot(df, aes(x='category', y='value', fill='category')) + geom_boxplot(alpha=0.7))
(ggplot(df, aes(x='category', y='value', fill='category')) + geom_boxplot(alpha=0.7))
Out[22]:
Violin Plots¶
In [23]:
Copied!
(ggplot(df, aes(x='category', y='value', fill='category')) + geom_violin(alpha=0.6))
(ggplot(df, aes(x='category', y='value', fill='category')) + geom_violin(alpha=0.6))
Out[23]:
In [24]:
Copied!
x = np.linspace(0, 10, 100)
df = pd.DataFrame({'x': x, 'y': np.sin(x) + 1.5})
(ggplot(df, aes(x='x', y='y')) + geom_area(fill='lightblue', alpha=0.7))
x = np.linspace(0, 10, 100)
df = pd.DataFrame({'x': x, 'y': np.sin(x) + 1.5})
(ggplot(df, aes(x='x', y='y')) + geom_area(fill='lightblue', alpha=0.7))
Out[24]:
Stacked Area¶
In [25]:
Copied!
df = pd.DataFrame({
'x': np.tile(np.linspace(0, 10, 50), 3),
'y': np.abs(np.concatenate([
np.sin(np.linspace(0, 10, 50)),
0.5 * np.cos(np.linspace(0, 10, 50)) + 0.5,
0.3 * np.sin(2 * np.linspace(0, 10, 50)) + 0.3
])),
'group': np.repeat(['A', 'B', 'C'], 50)
})
(ggplot(df, aes(x='x', y='y', fill='group')) + geom_area(alpha=0.6))
df = pd.DataFrame({
'x': np.tile(np.linspace(0, 10, 50), 3),
'y': np.abs(np.concatenate([
np.sin(np.linspace(0, 10, 50)),
0.5 * np.cos(np.linspace(0, 10, 50)) + 0.5,
0.3 * np.sin(2 * np.linspace(0, 10, 50)) + 0.3
])),
'group': np.repeat(['A', 'B', 'C'], 50)
})
(ggplot(df, aes(x='x', y='y', fill='group')) + geom_area(alpha=0.6))
Out[25]:
Step Charts¶
In [26]:
Copied!
x = np.linspace(0, 10, 20)
df = pd.DataFrame({'x': x, 'y': np.sin(x)})
(ggplot(df, aes(x='x', y='y')) + geom_step(color='blue', size=2))
x = np.linspace(0, 10, 20)
df = pd.DataFrame({'x': x, 'y': np.sin(x)})
(ggplot(df, aes(x='x', y='y')) + geom_step(color='blue', size=2))
Out[26]:
Ribbon Charts¶
Confidence bands or ranges:
In [27]:
Copied!
x = np.linspace(0, 10, 50)
y = np.sin(x)
df = pd.DataFrame({
'x': x,
'ymin': y - 0.3,
'ymax': y + 0.3
})
(ggplot(df, aes(x='x', ymin='ymin', ymax='ymax')) + geom_ribbon(fill='steelblue', alpha=0.3))
x = np.linspace(0, 10, 50)
y = np.sin(x)
df = pd.DataFrame({
'x': x,
'ymin': y - 0.3,
'ymax': y + 0.3
})
(ggplot(df, aes(x='x', ymin='ymin', ymax='ymax')) + geom_ribbon(fill='steelblue', alpha=0.3))
Out[27]:
Heatmaps¶
In [28]:
Copied!
x = np.arange(10)
y = np.arange(10)
X, Y = np.meshgrid(x, y)
Z = np.sin(X / 2) * np.cos(Y / 2)
df = pd.DataFrame({
'x': X.flatten(),
'y': Y.flatten(),
'z': Z.flatten()
})
(ggplot(df, aes(x='x', y='y', fill='z')) + geom_tile() + scale_fill_viridis_c())
x = np.arange(10)
y = np.arange(10)
X, Y = np.meshgrid(x, y)
Z = np.sin(X / 2) * np.cos(Y / 2)
df = pd.DataFrame({
'x': X.flatten(),
'y': Y.flatten(),
'z': Z.flatten()
})
(ggplot(df, aes(x='x', y='y', fill='z')) + geom_tile() + scale_fill_viridis_c())
Out[28]:
Text Labels¶
In [29]:
Copied!
df = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [2, 4, 3, 5],
'label': ['Point A', 'Point B', 'Point C', 'Point D']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=10, color='steelblue')
+ geom_text(aes(label='label'), vjust=-1))
df = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [2, 4, 3, 5],
'label': ['Point A', 'Point B', 'Point C', 'Point D']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=10, color='steelblue')
+ geom_text(aes(label='label'), vjust=-1))
Out[29]:
In [30]:
Copied!
df = pd.DataFrame({'x': np.random.randn(100), 'y': np.random.randn(100)})
(ggplot(df, aes(x='x', y='y'))
+ geom_point()
+ geom_hline(data=0, color='red', linetype='dash')
+ geom_vline(data=0, color='blue', linetype='dash'))
df = pd.DataFrame({'x': np.random.randn(100), 'y': np.random.randn(100)})
(ggplot(df, aes(x='x', y='y'))
+ geom_point()
+ geom_hline(data=0, color='red', linetype='dash')
+ geom_vline(data=0, color='blue', linetype='dash'))
Out[30]:
Slope/Intercept Lines¶
In [31]:
Copied!
df = pd.DataFrame({'x': range(10), 'y': [i * 2 + 1 for i in range(10)]})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=8)
+ geom_abline(slope=2, intercept=1, color='red', linetype='dash')
+ geom_abline(slope=1.5, intercept=3, color='blue'))
df = pd.DataFrame({'x': range(10), 'y': [i * 2 + 1 for i in range(10)]})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=8)
+ geom_abline(slope=2, intercept=1, color='red', linetype='dash')
+ geom_abline(slope=1.5, intercept=3, color='blue'))
Out[31]:
Jittered Points¶
Avoid overplotting for categorical x-axes:
In [32]:
Copied!
df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.random.randn(150)
})
# Without jitter - points overlap
(ggplot(df, aes(x='category', y='value'))
+ geom_point(alpha=0.5)
+ labs(title='Without Jitter'))
df = pd.DataFrame({
'category': np.repeat(['A', 'B', 'C'], 50),
'value': np.random.randn(150)
})
# Without jitter - points overlap
(ggplot(df, aes(x='category', y='value'))
+ geom_point(alpha=0.5)
+ labs(title='Without Jitter'))
Out[32]:
In [33]:
Copied!
# With jitter - points spread out
(ggplot(df, aes(x='category', y='value'))
+ geom_jitter(width=0.2, alpha=0.5)
+ labs(title='With Jitter'))
# With jitter - points spread out
(ggplot(df, aes(x='category', y='value'))
+ geom_jitter(width=0.2, alpha=0.5)
+ labs(title='With Jitter'))
Out[33]:
Rug Plots¶
Add marginal tick marks:
In [34]:
Copied!
df = pd.DataFrame({'x': np.random.randn(100), 'y': np.random.randn(100)})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(alpha=0.5)
+ geom_rug(sides='bl', alpha=0.3)
+ labs(title='Scatter with Marginal Rugs'))
df = pd.DataFrame({'x': np.random.randn(100), 'y': np.random.randn(100)})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(alpha=0.5)
+ geom_rug(sides='bl', alpha=0.3)
+ labs(title='Scatter with Marginal Rugs'))
Out[34]:
Point Borders (stroke)¶
Add borders to points with the stroke parameter:
In [35]:
Copied!
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 4, 3, 5, 4],
'category': ['A', 'B', 'A', 'B', 'A']
})
# Points with colored borders
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=15, stroke=2)
+ labs(title='Points with Borders (stroke=2)'))
df = pd.DataFrame({
'x': [1, 2, 3, 4, 5],
'y': [2, 4, 3, 5, 4],
'category': ['A', 'B', 'A', 'B', 'A']
})
# Points with colored borders
(ggplot(df, aes(x='x', y='y', color='category'))
+ geom_point(size=15, stroke=2)
+ labs(title='Points with Borders (stroke=2)'))
Out[35]:
Rectangles (geom_rect)¶
Highlight regions with rectangles:
In [36]:
Copied!
# Data for scatter plot
scatter_df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100)
})
# Rectangle to highlight a region
rect_df = pd.DataFrame({
'xmin': [-1], 'xmax': [1],
'ymin': [-1], 'ymax': [1]
})
(ggplot(scatter_df, aes(x='x', y='y'))
+ geom_rect(data=rect_df, mapping=aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax'),
fill='yellow', alpha=0.3)
+ geom_point(alpha=0.6)
+ labs(title='Scatter with Highlighted Region'))
# Data for scatter plot
scatter_df = pd.DataFrame({
'x': np.random.randn(100),
'y': np.random.randn(100)
})
# Rectangle to highlight a region
rect_df = pd.DataFrame({
'xmin': [-1], 'xmax': [1],
'ymin': [-1], 'ymax': [1]
})
(ggplot(scatter_df, aes(x='x', y='y'))
+ geom_rect(data=rect_df, mapping=aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax'),
fill='yellow', alpha=0.3)
+ geom_point(alpha=0.6)
+ labs(title='Scatter with Highlighted Region'))
Out[36]:
Labels with Background (geom_label)¶
Text labels with a background box for better readability:
In [37]:
Copied!
df = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [2, 4, 3, 5],
'label': ['Alpha', 'Beta', 'Gamma', 'Delta']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=10, color='steelblue')
+ geom_label(aes(label='label'), fill='white', alpha=0.8)
+ labs(title='Points with Background Labels'))
df = pd.DataFrame({
'x': [1, 2, 3, 4],
'y': [2, 4, 3, 5],
'label': ['Alpha', 'Beta', 'Gamma', 'Delta']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=10, color='steelblue')
+ geom_label(aes(label='label'), fill='white', alpha=0.8)
+ labs(title='Points with Background Labels'))
Out[37]:
Segments with Arrows¶
Add arrows to line segments:
In [38]:
Copied!
df = pd.DataFrame({
'x': [1, 2, 3],
'y': [1, 2, 1],
'xend': [2, 3, 4],
'yend': [2, 1, 2]
})
(ggplot(df, aes(x='x', y='y', xend='xend', yend='yend'))
+ geom_segment(arrow=True, arrow_size=15, color='steelblue', size=2)
+ labs(title='Segments with Arrows'))
df = pd.DataFrame({
'x': [1, 2, 3],
'y': [1, 2, 1],
'xend': [2, 3, 4],
'yend': [2, 1, 2]
})
(ggplot(df, aes(x='x', y='y', xend='xend', yend='yend'))
+ geom_segment(arrow=True, arrow_size=15, color='steelblue', size=2)
+ labs(title='Segments with Arrows'))
Out[38]:
Fixed Aspect Ratio (coord_fixed)¶
Ensure equal scaling on both axes:
In [39]:
Copied!
# Circle should appear as a circle, not an ellipse
theta = np.linspace(0, 2*np.pi, 100)
circle = pd.DataFrame({
'x': np.cos(theta),
'y': np.sin(theta)
})
(ggplot(circle, aes(x='x', y='y'))
+ geom_path(size=2)
+ coord_fixed(ratio=1)
+ labs(title='Circle with coord_fixed(ratio=1)'))
# Circle should appear as a circle, not an ellipse
theta = np.linspace(0, 2*np.pi, 100)
circle = pd.DataFrame({
'x': np.cos(theta),
'y': np.sin(theta)
})
(ggplot(circle, aes(x='x', y='y'))
+ geom_path(size=2)
+ coord_fixed(ratio=1)
+ labs(title='Circle with coord_fixed(ratio=1)'))
Out[39]:
Reversed Axes¶
Flip axis direction with scale_x_reverse() or scale_y_reverse():
In [40]:
Copied!
df = pd.DataFrame({'x': range(1, 6), 'y': [2, 4, 3, 5, 4]})
# Reversed y-axis (useful for rankings, depth charts)
(ggplot(df, aes(x='x', y='y'))
+ geom_line(size=2)
+ geom_point(size=10)
+ scale_y_reverse()
+ labs(title='Reversed Y-Axis', y='Rank (1 = best)'))
df = pd.DataFrame({'x': range(1, 6), 'y': [2, 4, 3, 5, 4]})
# Reversed y-axis (useful for rankings, depth charts)
(ggplot(df, aes(x='x', y='y'))
+ geom_line(size=2)
+ geom_point(size=10)
+ scale_y_reverse()
+ labs(title='Reversed Y-Axis', y='Rank (1 = best)'))
Out[40]:
LaTeX Labels (parse)¶
Render mathematical notation with parse=True:
In [41]:
Copied!
df = pd.DataFrame({
'x': [1, 2, 3],
'y': [1, 4, 9],
'label': [r'$x^2$', r'$\sqrt{x}$', r'$\frac{1}{x}$']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=15)
+ geom_text(aes(label='label'), parse=True, vjust=-1.5, size=14)
+ labs(title='Mathematical Labels with LaTeX'))
df = pd.DataFrame({
'x': [1, 2, 3],
'y': [1, 4, 9],
'label': [r'$x^2$', r'$\sqrt{x}$', r'$\frac{1}{x}$']
})
(ggplot(df, aes(x='x', y='y'))
+ geom_point(size=15)
+ geom_text(aes(label='label'), parse=True, vjust=-1.5, size=14)
+ labs(title='Mathematical Labels with LaTeX'))
Out[41]: