Copy
View this email in your browser
Subscribed by mistake? Unsubscribe
DUTC WEEKLY
Issue no. 94
July 20th,  2022

Issue Preview


1. Front Page: How Do You Iterate with Uncertainty?
2. Event Notice: More of What You Need to Succeed
3. News Article: Boost Your Skills—Become an Expert!
4. Cameron's Corner
5. Community Calendar

How Do You Iterate with Uncertainty?


Coming up on July 22nd, we're hosting a BONUS seminar in the "JavaScript and React for Data Scientists" series! Our goal for this session is to figure out how to work rapidly and effectively in this new environment.

We will share with you a collection of tips, tricks, and general practices for iterating quickly when developing sites with JavaScript and React.

We will discuss questions such as:
  • how do I iterate on building a component with user-interaction?
  • how do I iterate on visual tweaks and fit-and-finish items?
  • what is Typescript? how can it be useful?
  • how do I write simple tests?
  • how do I iterate without a browser? what is server-side rendering?
And, don't forget: you can still purchase a VIP ticket to get HD edited videos from all four sessions, notes and study materials, and access to an invite-only VIP session with our instructors.
 
It's not too late to sign up. Get your tickets now!
FRI, JUL 22, 2022 - Iterating with Uncertainty in JavaScript and React (JS & React Bonus)
FRI, JUL 22, 2022 - Iterating with Uncertainty in JavaScript and React (JS & React Bonus)
$0.00 - $399.00
Tickets
FRI, JUL 29, 2022 - The End-Users, How Much Can They Demand? (JS & React III)
FRI, JUL 29, 2022 - The End-Users, How Much Can They Demand? (JS & React III)
$0.00 - $399.00
Tickets

More of What You Need to Succeed


How do things really work? What's really going on behind the scenes? How much of this really matters; how much does one *really* need to know to be effective?

If you've ever asked yourself any of these questions in reference to the various mechanics, features, and functionality in Python, then this seminar series is for you.

You're really going to want to attend:
 

(More) Python Basics for Experts
DUTC Seminar Series for August, 2022


In this seminar series, we will pick up where we left off in our first “Python Basics for Experts” series held in December 2021. In that series, we answered questions about the built-in data types, the core language syntax, and major features. We started each question by presenting the most basic perspective —the thing that catches your curiosity, that makes you go “hmm,” that gets you digging—then discussed mechanics, motivations, metaphors, and meaning from the perspective of an expert, finally wrapping back around to how each of these questions and each of these topics will have immediate, material impact on your code, irrespective of whether you are a beginner or an expert!

Join us next month as we continue to think deeply about the "why" behind Python's design and features. You'll see tools you're already familiar with in a whole new light!
FRI, AUG 5, 2022 - (More) Python Basics for Experts I - How are things designed?
FRI, AUG 5, 2022 - (More) Python Basics for Experts I - How are things designed?
$0.00 - $399.00
Tickets
FRI, AUG 12, 2022 - (More) Python Basics for Experts II - How are things created?
FRI, AUG 12, 2022 - (More) Python Basics for Experts II - How are things created?
$0.00 - $399.00
Tickets
THU, AUG 18, 2022 - (More) Python Basics for Experts BONUS - How did you (even) know this?
THU, AUG 18, 2022 - (More) Python Basics for Experts BONUS - How did you (even) know this?
$0.00 - $399.00
Tickets
FRI, AUG 19, 2022 - (More) Python Basics for Experts III - How are things executed?
FRI, AUG 19, 2022 - (More) Python Basics for Experts III - How are things executed?
$0.00 - $399.00
Tickets

Boost Your Skills—Become an Expert!


Expertise is not about meticulous, rote study of APIs and syntax. It’s about thinking deeply, finding meaning, and building the right mental models. It’s about judicious study of details and using them to develop and corroborate fundamental concepts that allow you to methodically reason your way from problem statement to working solution.

In this course, attendees will engage in a total of ten hours of hands-on live-coding and another four hours in discussion sessions with James. Together we will uncover these concepts and mental models by finding and collecting the most critical details, then tying them together into larger conceptual frameworks which can be immediately applied to solve real problems in your work.

You do not need to already be a budding expert to benefit from this class. This class is about exploring the path to becoming an expert!

Here is James Powell, our Lead Instructor, addressing anyone who doesn't feel like an expert:
"I'm not even close to being an expert."
To thank you for joining us on your Python journey, we're offering a $200 off discount to the first three people who discount code "Thank_You_200" at checkout or use this link to sign up! 

We hope to see you soon!



Cameron’s Corner: 

Working with Long Labels In Matplotlib

Hey all!

I came across a fun blog post written by Andrew Heiss covering how to work with long tick labels in R's ggplot2. I couldn't resist the urge to recreate the visualizations in
matplotlib and wanted to share with you how you can deal with long tick labels in Python!

First, we’ll need some data. Using the same source as the blog post from above, we can fetch and process our data like so:
from pandas import read_csv

s = (
    read_csv('https://datavizs22.classes.andrewheiss.com/projects/04-exercise/data/EssentialConstruction.csv')
    .groupby('CATEGORY')['CATEGORY'].count()
    .sort_values(ascending=False)
)

print(s)
For most of these examples, you'll note that I opt to use the matplotlib API in favor of pandas' .plot api. This is primarily because pandas applies some transformations to the visualizations that I do not want. Instead, I want to highlight how one can use matplotlib to explicitly perform visual transformations rather than letting some other package handle it for you.

If you want complete control over your visualizations,
matplotlib is the tool for you.

Next, we want to apply some default settings for our plots. I'm opting to use a slightly larger font size (this is all about large labels right?), a slightly shorter figure size (don’t want inline plots being too large), removing the top and right spines, and a left-aligned title for our plots.
from matplotlib.pyplot import rc
from matplotlib import rcdefaults

rcdefaults()
rc('font', size=12)
rc('figure', figsize=(8,2))
rc('axes', titlesize=16, titlelocation='left')
rc('axes.spines', top=False, right=False)
With our defaults set, we are ready to make some figures! Let's take a look at our default barplot.
 

Default Barplot

from matplotlib.pyplot import subplots

fig, ax = subplots()
ax.bar(s.index, s)
ax.set_title('Default barplot')
Oh no! Look at those extremely overlapping xtick labels. It's almost impossible to make out any of those labels individually. Let's take a look at some techniques we can use to fit these labels in the figure so they're legible.

Manually Recoded Labels


Our first fix involves manually recoding our labels. We can reliably do this by performing a transformation on our data using a dictionary that maps old labels to new ones. These new labels are designed to either be shorter or have built-in line-breaks that help make the tick labels more clear.
# Manually Recode
fig, ax = subplots()
new_names = {
    'Approved Work': 'App. Work',
    'Affordable Housing': 'Aff. House',
    'Hospital / Health Care': 'Hosp\n& Health',
    'Public Housing': 'Pub. Hous.',
    'Homeless Shelter': 'Homeless\nShelter'
}
ax.bar(s.rename(new_names).index, s)
ax.set_title('Manually Recoded')
That seemed to work fairly well! The labels are no longer overlapping (though in some cases they're quite close).

Let's move onto our next approach and see how else we can account for long tick labels.

Wider Plot


Widening the plot (or figure) is a fairly straightforward approach that works if you only have a single plot on your figure. When you have a layout of multiple plots, you need to be careful that you are not unintentionally stretching the other plots, and you may need to use a subplot manager such as a GridSpec to ensure the plot you want to widen has access to more space on your figure than other plots.

You can always shrink the font sizes as well (since creating a larger plot effectively shrinks text).
fig, ax = subplots(figsize=(18, 2))
ax.bar(s.index, s)
ax.set_title('Wider Plot')

Swap x and y- axes


One of the most straightforward ways to account for long text labels is to simply change the orientation of the plot. English text is read horizontally, and we can maintain this readable layout by simply reorienting the plot enabling more horizontal space for our labels.
# Swap x and y
fig, ax = subplots()
ax.barh(s.index, s)

# our underlying data is already sorted, 
#  we need to invert the yaxis to ensure bars are
#  ordered longest to shortest
ax.invert_yaxis()
ax.set_title('Swap X & Y- axes')

Rotate the Labels


Following a similar approach to transposing our plot (swapping x & y), we can also create more horizontal space for our labels by rotating them. By doing this, we prevent the text from overlapping with one another by "parking" them like cars in a parking lot. Additionally, keeping them rotated nearly horizontal helps maintain legibility.

Two arguments I want to highlight here are the
ha (horizontal alignment) and the rotation_mode. By setting ha='right', we are informing matplotlib that we want the right side of the label to line up against the tick. This means that when we rotate it, the right side of the label will still line up with the tick itself. If we did not do this, our rotated text would be center-aligned against the bar it corresponds to, introducing overlap with other artists and ambiguity as to which label corresponds to which bar.

The
rotation_mode is more of a fit-and-finish argument. Essentially, rotation_mode determines whether the label is rotated and then aligned to the xtick (default), or the label is aligned and then rotated around the point of alignment (anchor). For our use case here, the latter is more useful at ensuring our xtick labels remain close to their corresponding ticks.
from matplotlib.pyplot import setp

fig, ax = subplots()

ax.bar(s.index, s)
setp(ax.get_xticklabels(), rotation=20, ha='right', rotation_mode='anchor');

# setp is a convenience for setting properties on an 
#   Artist or a list of Artist objects equivalent code below
# for text in ax.get_xticklabels():
#   text.set(rotation=20, ha='right', rotation_mode='anchor')

ax.set_title('Rotate Labels')

Dodge Labels


In contrast to some of the above approaches that aim to increase the amount of horizontal space, "dodging" the labels allows us to more effectively use the vertical space our labels have access to. Unfortunately, dodging is not a built-in feature of matplotlib, so the implementation here is a little hacky. We essentially take every other label and move it down such that it won't overlap with its immediately-adjacent labels. An important point here is that even though we implemented a dodge on every other label, we still have issues determining which label corresponds to which tick ultimately reducing the usefulness of this approach.
fig, ax = subplots()

ax.bar(s.index, s)
for text in ax.get_xticklabels()[1::2]:
    text.set_y(-.2)
    
ax.set_title('Dodge Labels')

Text Wrapping


Last, but certainly not least, is using a great helper function from Python's built-in textwrap library to perform whitespace wrapping for us. We could achieve a similar result by replacing any whitespace with a newline character, but textwrap uniquely lets us specify a width where we want to insert newlines via textwrap.fill.

I also want to point out that
matplotlib has some built-in support for auto wrapping of text, but, since we can't specify a width parameter directly, I found it more convenient to specify the text wrapping manually.
from textwrap import fill

fig, ax = subplots()
ax.bar([fill(s, width=10) for s in s.index], s)

Wrap Up


And that takes us to the end of ways that we can work with long tick labels in matplotlib. I personally think the label rotation, axes swap, and text wrapping are the most successful methods in dealing with this type of problem. I also put all of these tricks into a single figure (and gist link) for you to use as a reference whenever you need to make some fine tweaks to your tick labels!

Community Calendar

DUTC: Iterating with Uncertainty in JavaScript and React (JS & React Bonus)
July 22, 2022

In this bonus seminar, our goal is to figure out how to work rapidly and effectively in this new environment.

We will share with you a collection of tips, tricks, and general practices for iterating quickly when developing sites with JavaScript and React.
DUTC: The End-Users, How Much Can They Demand? (JS & React III)
July 29, 2022

In this seminar, our goal is to consider aspects to these UIs that will inevitably have to be fast—after all, end-users are quite demanding, and the reward for good work is more work.
DUTC: (More) Python Basics for Experts I - How are things designed?
August 5, 2022

In our first seminar in this series, we will answer questions of motivation: why do certain things exist and how do we incorporate them into the design of our code?
DUTC: (More) Python Basics for Experts II - How are things created?
August 12, 2022

In our second seminar in this series, we will answer questions about the parts of the Python runtime you may not typically consider—e.g., the construction of modules on import, the construction of types via the class statement, the construction of functions via the def statement. How do these things come into being, and how can they affect our code?
DUTC: (More) Python Basics for Experts BONUS - How did you (even) know this?
August 18, 2022

In our third seminar in this series, we will answer questions about the execution of Python code—e.g., how does attribute lookup actually work, what is the descriptor protocol, how do memory management and garbage collection work, what are generators and asynchronous functions under the covers? … and, of course, why does this all matter?
DUTC: (More) Python Basics for Experts III - How are things executed?
August 19, 2022

In our third seminar in this series, we will answer questions about the execution of Python code—e.g., how does attribute lookup actually work, what is the descriptor protocol, how do memory management and garbage collection work, what are generators and asynchronous functions under the covers? … and, of course, why does this all matter?
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 Estonia 2022
August 25, 2022

This year’s PyCon will bring together 9 international speakers who will share their insights on how Pythonistas can better connect and collaborate within the Python ecosystem.
Python Nordeste 2022
August 26–28, 2022

From the community, for the community.

Python Nordeste 2022 was created to promote maximum diversity and inclusion during the three days of the event. The meeting aims to spread the language among universities, companies, civil society institutions and individuals in search of new knowledge, thus promoting the diffusion of the Free Software culture and stimulating development in the Northeast Region.
EuroSciPy 2022
August 29–September 2, 2022

The EuroSciPy meeting is a cross-disciplinary gathering focused on the use and development of the Python language in scientific research.

This event strives to bring together both users and developers of scientific tools, as well as academic research and state of the art industry.
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 SK 2022
September 9–11, 2022

The PyCon SK 2022 conference, which will be held in Bratislava, is an annual gathering of the community using and developing the open-source Python programming language.
DUTC: Developing Expertise in Python & pandas
September 14–October 10, 2022

This course is an intense, hands-on experience that can take anyone of any level of Python know-how to the mastery of an expert.
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.
PyCon Portugal 2022
September 24, 2022

Whether you are an experienced programmer, a hobby hacker or an absolute beginner, we'd love to welcome you to the Python community. PyCons are hosted all around the world by volunteers from local Python communities.

This is the FIRST edition ever of PyCon Portugal—join us!
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.