Show newer

Everything in #Python has a type... even things you might not expect to have a type.

Like None

>>> type(None)
<class 'NoneType'>

Or NotImplemented

>>> type(NotImplemented)
<class 'NotImplementedType'>

Or... type?

>>> type(type)
<class 'type'>

@s_gruppetta I find it very nice, it's replaced a lot of the crufty matplotlib boilerplate I've accumulated over the years for creating publication-ready figures. You can always drop back down to "raw" matplotlib if you need to do something very specific.

I'll be mostly posting about in the data analysis and science and related domains here…

…but for those learning to code, whatever the final aim, it's good to get a broad perspective

So here's something different to learn about or consolidate the key data structures in Python - lists, tuples, dictionaries, and sets

Here's the link to the article for those who want a "diversion" from more serious-looking exercises!

thepythoncodingbook.com/2021/1

I think a good way to show code (until we wait for all servers to have Markdown like , at least) is to show a generated code image (from one of several online tools, or screenshot if you must) and add the code as the ALT text/description of the image, as in the quoted toot.

If, as in this case, the code is too long for the ALT text (this will only happen for long code snippets like this) the code could go in an unlisted reply to the toot with the image (or in the same toot, if you prefer/have space)

I think it's useful to have both a properly-formatted version for those who just want to read code and text which people can copy-paste

gist and other solutions means you need to leave the site - not ideal, I think

Stephen Gruppetta  
Here’s the code for the plot in the previous post - put in your own image file, of course! You can get the text from the images’s ALT text

We are doing an episode on "Mastodon for #Python (and open source) devs".

We'll be live streaming it on Monday.

youtube.com/watch?v=LhBfMoR3bv

Hope you can join @mkennedy, @foosel, @simon, and @astrojuanlu

Well, the code was too long for the ALT text, so here's the code pasted here:

import matplotlib.pyplot as plt

image = plt.imread("gardens.jpeg")

# Create a figure using a 4x4 array.
# The "." indicates an empty space in the grid
fig, ax = plt.subplot_mosaic(
[["Colour", "Colour", "Colour", "Hist Red"],
["Colour", "Colour", "Colour", "Hist Green"],
["Colour", "Colour", "Colour", "Hist Blue"],
["Red", "Green", "Blue", "."]]
)

ax["Colour"].imshow(image)
ax["Colour"].axis("off")

# Plot histograms for red, green, and blue channels
ax["Hist Red"].hist(image[:, :, 0].ravel(), bins=50, color="red")
ax["Hist Red"].axis("off")
ax["Hist Green"].hist(image[:, :, 1].ravel(), bins=50, color="green")
ax["Hist Green"].axis("off")
ax["Hist Blue"].hist(image[:, :, 2].ravel(), bins=50, color="blue")
ax["Hist Blue"].axis("off")

# Extract red, green, and blue channels from image
image_red = image.copy()
image_red[:, :, 1:] = 0
image_green = image.copy()
image_green[:, :, 0] = 0
image_green[:, :, 2] = 0
image_blue = image.copy()
image_blue[:, :, :2] = 0

ax["Red"].imshow(image_red)
ax["Red"].axis("off")

ax["Green"].imshow(image_green)
ax["Green"].axis("off")

ax["Blue"].imshow(image_blue)
ax["Blue"].axis("off")

fig.show()

Show thread

Here's the code for the plot in the previous post - put in your own image file, of course!

You can get the text from the images's ALT text

Show thread

Have you used `subplot_mosaic()` in 's yet?

Or are you a dinosaur like me and still use older functions? After all, `subplot_mosaic()` was only introduced in 2020 in version 3.3

Recently, I decided to finally explore `subplot_mosaic()` and I know I'll never go back to whatever I did before to plot these types of figures!

<code in next post>

/1

@s_gruppetta your python coding book ist the best I ever read about coding ! Thank 🙏🏼you so much 🙏🏼for the free version online. So many things I finally understand - you did an amazing job!

Since moving from to all those years ago, I had used `meshgrid()` the "non-ideal" way for so long–it was just habit from many years of using it in MATLAB

But now I know better, and my memory usage will be happy in so many cases!!

thepythoncodingbook.com/2022/0

Show thread

If you've come to directly from , as I have, and you used to use `meshgrid` in MATLAB, you possibly use it in exactly the same way in Python?

I did for a very long time…

But Python has more options which can save you execution time and memory

Here's my exploration of 's and other related functions

thepythoncodingbook.com/2022/0

#introduction
I'm Mariatta, in Vancouver 🇨🇦

I work at Google as Senior Developer Relations Engineer, maintaining open source Python client libraries for Google Cloud.

As one of the #Python core developers, I care about documentation, workflow, bots, and contributor experience.

I'm the Chair of #PyConUS 2023-2024, Fellow member of @ThePSF and Community Service Award recipient.

My pets are a variety of tropical freshwater fish and some Amano shrimps.

I love food, traveling, and kdrama 👋

Hi there,
#introduction

This is the official Mastodon account for the Python Software Foundation (PSF.) We're the non-profit home of the Python programming language and our mission is to to promote, protect, and advance the Python programming language, and to support and facilitate the growth of a diverse and international community of Python programmers.

We also run PyCon US, us.pycon.org/2023/

We're excited to be here. :D

Hi new followers. We just got @talkpython officially on Mastodon. Please follow if you're interested in the show.

Hi #Python people. Made it to FOSS Mastodon. Please connect if you like the podcast.

My favourite textbook of all time – had been in storage for a while as we did a long, convoluted, house move…

It's back, safe and sound, nicely weathered and thumbed from all those years of use…

---

PS: just realised it's my wife's copy, not mine. **I Want My One Back!** (It's still in a box, somewhere…)

@s_gruppetta This kind of dynamic 3D plotting is very useful for illustrating physics concepts like your example here, but in business applications it's indeed much less common and, in general, not necessary (at least in my company). But the fact that it's not used all over the place only enhances the coolness of this example 😉 I will certainly enjoy this!

@s_gruppetta this is really awesome! Given that it's for learning the coding side, I won't mark you down for using explicit integration 😜.

It's amazing how dynamic visualization can really solidify complex topics. Even when I've done iterative static computation in the past, dynamically-updating matplotlib plots really helped give me more intuition about the problem I was trying to solve.

@s_gruppetta @mborus We'll update this server (ETA unknown) to import some of the useful features from the main branch (vanilla Mastodon). An edit button is one of those. :)

Show older
Qoto Mastodon

QOTO: Question Others to Teach Ourselves
An inclusive, Academic Freedom, instance
All cultures welcome.
Hate speech and harassment strictly forbidden.