Copy
View this email in your browser
Subscribed by mistake? Unsubscribe
DUTC WEEKLY
Issue no. 85
May 18th,  2022

Issue Preview


1. Front Page: ⚡️ FLASH SALE ⚡️
2. News Article: Lithuania, Here James Comes!
3. Cameron's Corner
4. Community Calendar

⚡️ FLASH SALE ⚡️


How fast can you think? You have the next 24 hours to prove your speed by purchasing any "Coding at the Speed of Thought" seminar tickets for 60% off! Just use discount code "FLASH60".

In this seminar series, we want to...
  • Cover the use of Python as applied to rapid evolution of programs from simple proofs-of-concept to complex prototypes. 
  • Discuss design decisions and a deliberate, methodical process that can be applied to any iterative design approach. 
  • Touch upon where various features and functionalities of Python have direct consequence to the maintainability and generalizability of your code.

Our focus will be to understand how to build complex programs from simple parts in a methodical, deliberate, iterative fashion making full use of all of the features of Python.

(We’re also going to have a lot of fun!)

Act now to take advantage of this limited-time offer!
THU, MAY 19, 2022 - Coding at the Speed of Thought ② Jumble, Crossword, Scrabble
THU, MAY 19, 2022 - Coding at the Speed of Thought ② Jumble, Crossword, Scrabble
$0.00 - $399.00
Tickets
FRI, MAY 20, 2022 - Coding at the Speed of Thought ③ Battleship
FRI, MAY 20, 2022 - Coding at the Speed of Thought ③ Battleship
$0.00 - $399.00
Tickets
THU, MAY 26, 2022 - Coding at the Speed of Thought Ⓑ Bonus Session! (Wordle!)
THU, MAY 26, 2022 - Coding at the Speed of Thought Ⓑ Bonus Session! (Wordle!)
$0.00 - $399.00
Tickets

Lithuania, Here James Comes!


Our Lead Instructor, James Powell, is prepping to travel to Lithuania to be a keynote speaker at PyCon LT 2022 from May 26–27! His talk will be titled, int is to list as float is to tuple.

This event is in-person only: join us in Lithuania to see what great talk James has in store! If you can't make it to the event, keep an eye on DUTC Weekly—we'll send a link to the recording as soon as it's available.

For more information, check out the PyCon LT 2022 website.


Cameron’s Corner
 

Matplotlib: Place It Where You Want It

I recently delivered a few seminars on matplotlib in which I demonstrated how to conceptually approach its two APIs, convenience layers vs essential layers, dichotomous artist types, and coordinate systems/transforms. Once you understand these ideas, the entire utility of matplotlib begins to snap into place.

This week, I want to highlight one of these concepts: coordinate systems & transforms. The first step to making an aesthetically appealing graphic is to have confidence in placing Artists where you want them. Their existence (or lack thereof) on your Figure should not be a surprise, and by understanding matplotlib's coordinate systems, we gain more control over the aesthetic of our plots.

 

Coordinate Systems

 
Matplotlib has many coordinate systems, but in my experience, the two most important ones are the "data" and "axes" coordinate systems. These both refer to coordinate systems that exist within an Axes

In matplotlib, the "axes" system represents a proportional coordinate system within the Axes. This space ranges from 0-1 on its horizontal and vertical dimensions, wherein the origin (0, 0) is the lower left corner, increasing to the right and up, respectively, as the values increase to (1, 1). This means that a coordinate of (.3, .8) is 30% from the left side of the Axes and 80% from the bottom.

The "data" coordinate system is also embedded within the axes. However, this coordinate system is bound by the values of the x and y axis of a given Axes instance instead of 0 and 1.


Transforms

 
coordinate transform display purpose
“axes” ax.transAxes display an artist on an Axes in an absolute and static position
“data” ax.transData display an artist on an Axes according to the values of the x- and y-axes

We typically think of Axes as primarily containing a data-space. When we run the following code, we create a plot that has a line which exists in data-space. The points we feed into the line as x- and y-coordinates are drawn according to the x and y-axis of our Axes.

These coordinate systems are important because they control where Artists are placed on our axes. We can control whether a given Artist is drawn according to "axes" or "data" coordinates via its transform parameter. In general the use-cases for these transforms as follows:
from matplotlib.pyplot import subplots
from numpy import arange

xs = arange(1, 10)

fig, ax = subplots()
ax.plot(xs, xs ** 2, label="line")
But, what if we wanted to draw something on these axes that ignored data-space, like Text that always appears in the upper left corner of the plot?

To insert Artists in a static position on the Axes, we need to use the axes proportional coordinate system, not the axes data coordinate system.
from matplotlib.pyplot import subplots
from numpy import arange

# Aesthetics
rcParams['font.size'] = 12
for side in ['left', 'right', 'top', 'bottom']:
    rcParams[f'axes.spines.{side}'] = True

fig, axes = subplots(1, 2, figsize=(12, 6))

for ax, max_x in zip(axes.flat, [10, 20]):
    xs = arange(1, max_x)

    ax.plot(xs, xs ** 2, label="line")

    ax.text(
        x=.05, y=.95,
        s='proportional coords (.05, .95)',
        transform=ax.transAxes
    )

    # ax.text defaults to using transform=ax.transData
    ax.text(
        x=5, y=20, 
        s='data coords (5, 20)', 
        transform=ax.transData
    )
On the plots above, you can see there is text in the upper left corner. By inputting x=.05, y=.95, ... transform=ax.transAxes, I was able to align the left side of that text to 5% across the x-axis, and 95% up the y-axis.

Contrarily, when I specified x=5, y=20, transform=ax.transData, I was able to insert text at the data location of (5, 20) on the x- and y-axes. This text label seemingly shifted places from the left Axes to the right Axes. This is because the scale of our x- and y-axes changed which shifts the overall placement of the text.

We can use this idea of transforms and coordinate systems to create very powerful visualizations. In fact, I used this idea to replicate a plot I encountered on Twitter: I created multiple nested Axes on a blended coordinate system to draw histograms that overlap with bars from a bar chart as a way to highlight the average value and spread of a couple datasets.
from matplotlib.pyplot import subplots, show, rcParams
from numpy.random import default_rng
from pandas import DataFrame

# Aesthetics: increase font size & remove spines
rcParams['font.size'] = 16
for side in ['left', 'right', 'top', 'bottom']:
    rcParams[f'axes.spines.{side}'] = False

# Generate some data
rng = default_rng(0)

df = DataFrame({
    '$\mu=40,\sigma=3$': rng.normal(40, 3, size=300),
    '$\mu=65,\sigma=5$': rng.normal(65, 5, size=300),
    '$\mu=50,\sigma=8$': rng.normal(50, 10, size=300),
})
agg_df = df.mean()

fig, ax = subplots(figsize=(8,5))
bars = ax.bar(agg_df.index, agg_df)

for rect, label in zip(bars, df.columns):
    x_data_pos = rect.get_x()

    # ax.get_xaxis_transform() returns a "blended transform"
    #   this means that I can specify my x-coordinates
    #   in data coordinates/units, and my y-coordinate 
    #   in proportional coordinates/units 
    hist_ax = ax.inset_axes(
        (x_data_pos, 0, rect.get_width(), 1),
        transform=ax.get_xaxis_transform(),
        sharey=ax # share the y-axis with the parent `Axes` 
    )
    
    # draw a histogram from the dataset onto this new Axes
    hist_ax.hist(
        df[label], orientation='horizontal',
        ec='gray', fc='white',
        alpha=.6, rwidth=.9
    )

    # make the background of our child axes 
    #    invisible (or else it hides the bar)
    hist_ax.patch.set_alpha(0)
    hist_ax.xaxis.set_visible(False)
    hist_ax.yaxis.set_visible(False)

ax.patch.set_color('#f4f4f4')
ax.yaxis.grid(color='lightgray')
ax.yaxis.set_tick_params(width=0)
ax.set_axisbelow(True)

ax.set_title('Insetting a histogram on each bar\nof a bar chart')
And there you have it—leveraging matplotlib's coordinate systems through transform! By understanding this concept, you'll be able to place anything, anywhere on your plots. This opens the door to create powerful visualizations by having complete control of the placement of your annotations, legends, and other Artists.

This matplotlib binge has been a lot of fun, and I can't wait to share with you what visualizations I'll come up with next!

Community Calendar

DUTC: Coding at the Speed of Thought - Jumble, Crossword, Scrabble
May 19, 2022

Turning our attention to the Sunday newspaper, let’s play some word games—Scrabble, the Crossword, and the Jumble. Under strict time constraints, can we model these problems, solve them, and develop generalized library code for them?
DUTC: Coding at the Speed of Thought - Battleship
May 20, 2022

You sunk my Battleship! Let’s play a complex board game with multiple rules, moving parts, and entities. Under strict time constraints, let’s see how we can rapidly develop small proofs-of-concept and connect them together into a fully functional working prototype. Can we do it? Can we sink the battleship?
Django Girls 2022
May 21, 2022

If you are a woman, know English and have a laptop, you can apply for this event to learn how to build a website! You don't need to know any technical stuff – this workshop is for people who are new to programming.
DUTC: Coding at the Speed of Thought - Wordle
May 26, 2022

Wordle has been the craze of early 2022. Let’s explore how we can model Wordle in our own terminal from its core mechanics to its minute details. We'll also examine how well we've prototyped this game as we tweak the engine to create some of Wordle's most popular (and fun) spin-offs!
PyCon LT 2022
May 26–27, 2022



DUTC's James Powell will be attending as a keynote speaker!

PyCon LT is a community event that brings together new and experienced Python users. Their goals are to grow and support a community of Python users, encourage learning and knowledge sharing, and popularize Python tools/libraries and open source in general. You can find more information on their website or Facebook page.
PyCon IT 2022
June 2–5, 2022

PyCon Italia is the Italian conference on Python. Organised by Python Italia, it has now become one of the most important Python conferences in Europe. With over 700 attendees, the next edition will be the 12th.
PyData London 2022
June 17–19, 2022



DUTC's James Powell will be attending, giving a talk, and hosting the PubQuiz!

PyData London 2022 is a 3-day event for the international community of users and developers of data analysis tools to share ideas and learn from each other. The global PyData network promotes discussion of best practices, new approaches, and emerging technologies for data management, processing, analytics, and visualization.
GeoPython 2022
June 20–22, 2022

The conference is focused on Python and Geo, its toolkits and applications. GeoPython 2022 will be the continuation of the yearly conference series. The conference started in 2016 and this is now the 7th edition. Key subjects for the conference series are the combination of the Python programming language and Geo.
EuroPython 2022
July 11–17, 2022

   

DUTC's James Powell and Cameron Riddell will be participating in the EuroPython mentorship program!

Welcome to the 21st EuroPython. We're the oldest and longest-running, volunteer-led Python programming conference on the planet! Join us in July in the beautiful and vibrant city of Dublin. We'll be together, face to face and online, to celebrate our shared passion for Python and its community!
Kiwi PyCon 2022
August 19–21, 2022

The first-ever Kiwi PyCon was held in Ōtautahi Christchurch, in 2009, the same year that New Zealand Python User Group was founded. The Garden City holds a very special place in the history of Python in New Zealand and Kiwi PyCon XI should have been held there in September 2020.  Along came the pandemic...

Somewhat later, they are back!

For more information, check out their official Twitter account.
PyCon APAC 2022
September 3–4, 2022

PyCon Taiwan is an annual convention in Taiwan for the discussion and promotion of the Python programming language. It is held by enthusiasts and focuses on Python technology and its versatile applications.
PyGeekle Summit'22
September 6–7, 2022

While offline events are temporarily stopped, Geekle never stops and is running the online PyGeekle Summit'22. Our speakers are leading experts from all over the world who are ready to share what challenges Python experts face in their work.
PyCon UK 2022
September 16–18, 2022

PyCon UK will be returning to Cardiff City Hall from Friday 16th to Sunday 18th September 2022.
More details coming soon!
DjangoCon Europe 2022
September 21–25, 2022

This is the 14th edition of the Conference and it is organized by a team made up of Django practitioners from all levels. We welcome people from all over the world.

Our conference seeks to educate and develop new skills, best practices, and ideas for the benefit of attendees, developers, speakers, and everyone in our global Django Community, not least those watching the talks online.
For more community events, check out python.org's event page.

Have any community events you'd like to see in the Community Calendar? Submit your suggestions to newsletter@dutc.io!
Twitter
Website
Copyright © 2022 Don't Use This Code, All rights reserved.