Skip to content

Instantly share code, notes, and snippets.

@sjtalkar
Last active February 13, 2021 16:05
Show Gist options
  • Save sjtalkar/2f01a542f5b0f96082875ddc0f517a30 to your computer and use it in GitHub Desktop.
Save sjtalkar/2f01a542f5b0f96082875ddc0f517a30 to your computer and use it in GitHub Desktop.
Altair Emoji category chart within PowerBI
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
# dataset = pandas.DataFrame(Emoji, Use Percent, Use Number)
# dataset = dataset.drop_duplicates()
# Paste or type your script code here:
# The following code to create a dataframe and remove duplicated rows is always executed and acts as a preamble for your script:
import altair as alt
plot = (
alt.Chart(dataset)
.mark_square(color="red", strokeWidth=3,)
.encode(
x=alt.X("Emoji:N", axis=alt.Axis(title=None, labelFontSize=30, labelAngle=0)),
y=alt.Y(
"Use Percent:Q",
axis=alt.Axis(
format=".0%",
labelFontSize=12,
labelFontWeight="bold",
titleFontSize=20,
titleFontWeight="bold",
),
),
size=alt.Size(
"Use Percent:Q",
scale=alt.Scale(domain=[0, 0.3], range=[30, 500]),
legend=None,
),
color=alt.Color(
"Use Percent:Q",
scale=alt.Scale(range=["green", "yellow", "red"]),
legend=alt.Legend(format=".1%"),
),
)
.properties(width=600, height=400)
)
text = plot.mark_text(
align="left",
baseline="middle",
dx=10, # Nudges text to right so it doesn't appear on top of the bar
dy=-10,
).encode(
# we'll use the percentage as the text
text=alt.Text("Use Number:Q"),
size=alt.SizeValue(20),
)
# (text+plot).show()
(text + plot).save("chart.png")
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
img = mpimg.imread("chart.png")
imgplot = plt.imshow(img)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment