Skip to content

Commit cfa0f5a

Browse files
authored
Merge pull request #217 from RommelArtola/source
Added seaborn.objects line styling code to page needing more languages to it,
2 parents c2ffb9b + d1bd38b commit cfa0f5a

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
471 KB
Loading

Presentation/Figures/styling_line_graphs.md

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,119 @@ There are several ways of styling line graphs. The following examples demonstrat
1818

1919
# Implementation
2020

21+
## Python
22+
```python
23+
import pandas as pd
24+
import seaborn.objects as so
25+
import numpy as np
26+
import matplotlib.pyplot as plt
27+
from seaborn import axes_style
28+
29+
30+
31+
32+
# Download the economics dataset (from ggplot2 so comparison is apples-to-apples)
33+
url = "https://raw.githubusercontent.com/tidyverse/ggplot2/main/data-raw/economics.csv"
34+
economics = pd.read_csv(url)
35+
36+
37+
# Quick manipulation of dataframe to convert column to datetime
38+
df = (
39+
economics
40+
.assign(
41+
date = lambda df: pd.to_datetime(df['date'])
42+
)
43+
)
44+
45+
46+
47+
# Default plots (Notice the xaxis only has 2 years! We'll fix this in p2)
48+
p1 = (
49+
so.Plot(data=df, x='date', y='uempmed')
50+
.add(so.Line())
51+
)
52+
p1
53+
54+
55+
56+
57+
## Change line color and chart labels, and fix xaxis
58+
## Note here that color is inside of the Line call, so this would color the line.
59+
## If color were instead *inside* the so.Plot() object, SO would assign it
60+
## a different line for each value of the factor variable (column), colored differently. (Commonly referred to as hue in seaborn)
61+
# However, in our case, we can pass a color directly.
62+
p2 = (
63+
so.Plot(data=df, x='date', y='uempmed')
64+
.add(so.Line(color='purple'))
65+
.label(title='Median Duration of Unemploymeny', x='Date', y='')
66+
.scale(x=so.Temporal().tick(upto=10)) #Needed for current configuration of seaborn.objects so xaxis prints more than 2 ticks
67+
.theme(axes_style("whitegrid")) #use a function from parent seaborn library, that will pass a prebuilt selection based on what you pass
68+
)
69+
70+
p2
71+
72+
73+
74+
## plotting multiple charts (of different line types and sizes)
75+
p3 = (
76+
so.Plot(data=df)
77+
.add(so.Line(color='darkblue', linewidth=5), x='date', y='uempmed')
78+
.add(so.Line(color='red', linewidth=2, linestyle='dotted'), x='date', y='psavert')
79+
.label(title='Unemployment Duration (Blue)\n & Savings Rate (Red)',
80+
x='Date',
81+
y='')
82+
.scale(x=so.Temporal().tick(upto=10)) #Needed for current configuration of seaborn.objects so xaxis prints more than 2 ticks
83+
.theme(axes_style("whitegrid")) #use a function from parent seaborn library, that will pass a prebuilt selection based on what you pass
84+
)
85+
86+
p3
87+
88+
89+
## Plotting a different line type for each group
90+
## There isn't a natural factor in this data so let's just duplicate the data and make one up
91+
df['fac'] = 1
92+
df2 = df.copy()
93+
df2['fac'] = 2
94+
df2['uempmed'] = df2['uempmed'] - 2 + np.random.normal(size=len(df2))
95+
df_final = pd.concat([df, df2], ignore_index=True).astype({'fac':'category'})
96+
97+
98+
p4 = (
99+
so.Plot(data=df_final, x='date', y='uempmed', color='fac')
100+
.add(so.Line())
101+
.label(title = "Median Duration of Unemployment",
102+
x = "Date",
103+
y = "",
104+
color='Random Factor')
105+
.scale(x=so.Temporal().tick(upto=10)) #Needed for current configuration of seaborn.objects so xaxis prints more than 2 ticks
106+
.theme(axes_style("whitegrid")) #use a function from parent seaborn library, that will pass a prebuilt selection based on what you pass
107+
)
108+
109+
p4
110+
111+
112+
113+
# Plot all 4 plots
114+
fig, axs = plt.subplots(2, 2, figsize=(10, 8))
115+
# Draw each plot in the corresponding subplot
116+
p1.on(axs[0, 0]).plot()
117+
p2.on(axs[0, 1]).plot()
118+
p3.on(axs[1, 0]).plot()
119+
p4.on(axs[1, 1]).plot()
120+
121+
# Adjust layout to avoid overlap
122+
plt.tight_layout()
123+
124+
# Show the combined plot
125+
plt.show()
126+
```
127+
The four plots generated by the code are (in order p1, p2, then p3 and p4):
128+
129+
![Four Styled Line Graphs in Python]({{ "/Presentation/Figures/Images/Styling-Line-Graphs/styling_line_graphs_Python.png" | relative_url }})
130+
131+
132+
133+
21134
## R
22135
```r
23136
## If necessary

0 commit comments

Comments
 (0)