Skip to content

Instantly share code, notes, and snippets.

@fzakaria
Created April 13, 2023 22:56
Show Gist options
  • Save fzakaria/60a7eb49584d568943080f76210e233b to your computer and use it in GitHub Desktop.
Save fzakaria/60a7eb49584d568943080f76210e233b to your computer and use it in GitHub Desktop.
from plotnine import *
import pandas as pd
import numpy as np
# Initialize list of lists
raw = [['PHP', 9, 70], ['Java', 8, np.nan], ['JavaScript', 10, 683], ['.NET', 3, np.nan], ['Python', 6, 19], ['Ruby', 9, 68]]
data = {
'Language': [],
'Type': [],
'Value': []
}
for elem in raw:
data['Language'] += [elem[0], elem[0]]
data['Type'] += ['Direct', 'Indirect']
data['Value'] += [elem[1], elem[2]]
def no_labels(values):
return [""] * len(values)
# Create the pandas DataFrame
df = pd.DataFrame(data = data)
dodge_text = position_dodge(width=0.9)
plot = (ggplot(df, aes(x='Language', y='Value', fill='Type'))
+ geom_col(stat='identity', position='dodge')
+ scale_y_log10(labels=no_labels, expand=(0, 0, 0, 1))
+ geom_text(aes(label='Value'),
position=dodge_text,
size=8, va='bottom', format_string='{:0.0f}')
+ theme_minimal()
+ theme(panel_background=element_rect(fill='white'),
axis_text_y=element_blank(),
axis_title_y=element_blank(),
axis_ticks_major_y=element_blank(),
axis_title_x=element_blank(),
axis_line_x=element_line(size=1),
panel_grid_major=element_blank(),
panel_grid_minor=element_blank(),
legend_title=element_blank())
)
save_as_pdf_pages([plot])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment