Latest / Talk Python To Me / #384: Python Data Visualization - Where To Start?
Transcript
- 0:00Do you struggle to know where to start with the wide range of Python's data visualization
- 0:03frameworks? Not sure when to use Plotly versus Matplotlib versus Altair? Then this episode is
- 0:10for you. We have Chris Moffitt, a Talk Python course author and founder of Practical Business Python,
- 0:15back on the show to discuss getting started with Python's data visualization frameworks.
- 0:20This is Talk Python To Me, episode 384, recorded September 29th, 2022.
- 0:26Welcome to Talk Python To Me, a weekly podcast on Python. This is your host, Michael Kennedy.
- 0:44Follow me on Twitter where I'm @mkennedy and keep up with the show and listen to past episodes
- 0:48at talkpython.fm and follow the show on Twitter via at talkpython. We've started streaming most of our
- 0:55episodes live on YouTube. Subscribe to our YouTube channel over at talkpython.fm/youtube to get
- 1:01notified about upcoming shows and be part of that episode. This episode is sponsored by Microsoft for
- 1:07Startups Founders Hub. Check them out at talkpython.fm/founders hub to get early support for your
- 1:14startup. And it's brought to you by us over at Talk Python Training. Did you know we have one of the
- 1:20largest course libraries for Python courses? They're all available without a subscription. So check it
- 1:26out over at talkpython.fm. Just click on courses. Transcripts for this episode are sponsored by
- 1:32Assembly AI, the API platform for state-of-the-art AI models that automatically transcribe and understand
- 1:39audio data at a large scale. To learn more, visit talkpython.fm/assembly AI. Chris, welcome back to
- 1:47Talk Python To Me. Thank you. Glad to be here again. I'm glad to have you back. We originally
- 1:52had you on to talk about the work that you're doing at Practical Business Python. And looking at the page
- 1:59now, your last article, pandas group by warning on the 26th, which as of recording, two days ago,
- 2:05looks like you're still really active on Practical Business Python. I am. It's been a while. To be honest,
- 2:11I spent a lot of time working on the course that we'll talk about in a moment. And so some of this
- 2:15stuff fell by the wayside. And I think like everybody in the COVID times have been a weird
- 2:20time warp for us all. So I haven't spent as much time on it as I would like, but I am getting back
- 2:26into it. And as you mentioned, just put a short article up there that in some ways kind of encapsulates
- 2:33a lot of what I want to do with Practical Business Python is I'm writing in this specific article in general
- 2:38on the blog about problems that I encounter that I think can help other people. And this was a short
- 2:45article about some kind of gotcha behavior with group by that I've been bitten by a couple times.
- 2:51And this most recent time, I decided, you know what, I need to write about this and share it with people
- 2:57so that hopefully they're not going to fall into the same traps that I had.
- 3:00I love when you write something as kind of a note for yourself or a roadmap for yourself. And then
- 3:05later you go back and you search for it. And you're like, I got to remember how this went.
- 3:10And like hit number one as your result. You're like, okay, I guess I don't remember this,
- 3:15but I'm going back to it. And I, you know, my future self thanks my old self for it, right?
- 3:19Yes, exactly. And sometimes I even remember I wrote an article about it and will refer to it.
- 3:24And then a lot of times you're right. I'll do a Google search. I'm like, oh yeah,
- 3:27I did solve that before.
- 3:30How interesting. Yeah, cool. What a great resource. People should be definitely checking
- 3:34this out. If you know, you want to do data science, Python data science intersected with
- 3:38Python. Of course you were on the show a couple of times ago, way back on 2019 and the before times
- 3:44escaping Excel hell with Python and Pandas. Basically you were making the case for
- 3:49using the Python data science stack instead of Excel, right?
- 3:53Yes, absolutely. Yeah. And you know, the blog and a lot of my experience has been
- 3:58doing data analysis, data manipulation, data science, and trying to leverage the power of Python.
- 4:05And most people in a business setting, their go-to data science, data science tool or data analysis
- 4:12tool is Excel. And it has its place. I'm not advocating we get rid of Excel, but I think there
- 4:18are a lot of things that we can do with Python that are much quicker, much less error prone and
- 4:24much more efficient than trying to do it in Excel. And Excel is one of those tools where there's such a
- 4:30wide range of usage. There's some people that are experts and can do really complex, very efficient
- 4:36things in Excel, but there's a lot of people that treat Excel like the proverbial hammer. Everything's a
- 4:42nail. So you try and use Excel to do everything from data cleaning to building out your financial
- 4:47statements to, I don't know, machine learning. And it's probably not really the best tool for all of
- 4:53those things. I think Python really fills a nice niche and had the good fortune of using it for a
- 4:59lot of different types of activities in my business career and wanted to talk about that in the podcast.
- 5:06You know, one thing that occurred to me, just thinking about the code that I've seen for like
- 5:10visualizing data and so on with Python compared to writing algorithms or web apps or something,
- 5:18it seems to me that the amount of Python that you have to know and able to maybe import pandas,
- 5:24load a CSV and then graph it, you almost hardly need to know Python at all. And it's more about
- 5:30knowing the APIs of the various visualization frameworks like Mapplotlib or Seaborn, right?
- 5:35No, absolutely. I agree completely. I mean, you basically need to know, like you said, how to
- 5:39import and even backing up probably the most, the biggest challenge is getting Python set up on your
- 5:45system, depending on the system you have and everything, getting your environments all squared
- 5:49away. But once that's done, you're right. The data visualization libraries, no matter which one you
- 5:54choose, essentially are knowing how to call functions.
- 5:57Yeah, exactly. And they all often seem to have their own little DSL domain specific language for
- 6:03what they've decided they're going to do, right?
- 6:05Yes, exactly. And I think that's, you know, part of the challenge is everybody thinks a different way.
- 6:10And sometimes like a library might make a lot of sense to you, but other people, it doesn't. And so
- 6:16that's a lot of where I think some of the challenges in the visualization landscape are,
- 6:20are trying to find that right API that makes sense for the actual business problems or
- 6:24visualization problems that you have and how, how it fits in your brain.
- 6:28Indeed. So if people are coming from a business perspective and maybe Excel is where they or their
- 6:34colleagues have been working, I definitely recommend people go check out episode 200. And then there was
- 6:39also 10 tips to move from Excel to Python, a lot of common themes here.
- 6:43Yes.
- 6:44So normally at the beginning of the show, I ask people how they got into programming in Python.
- 6:48I've got you to answer that at least once, maybe twice, but probably a third time is,
- 6:54is not required. So just give us an update on what you've been up to.
- 6:57Sure. I am still working in the medical device industry. My job doesn't require Python, but my job
- 7:04does involve a lot of data analysis and working with not necessarily large sets of data, but sometimes
- 7:10data sets that are big enough that it's a little bit painful to work in Excel. And I just continue to
- 7:16find Python as the tool that I reach for when I need to do data analysis. And I continue to use it to
- 7:23build repeatable data analysis pipelines. I use it to clean data, maybe take external data that we buy
- 7:31from a third party and integrate it with internal data sets, or as we'll talk about doing data
- 7:38visualization. I think there are a lot of things that Python can do from a data visualization perspective
- 7:42that are easier than trying to use Excel or some of the other tools that are available in a traditional
- 7:49environment. So I continue to live and breathe Python. And I would say, you know, every week,
- 7:56I use it a little bit, some weeks, a lot more than others, but continue to enjoy kind of that blend of
- 8:02Python, real world problems, and not just for software development's sake.
- 8:07Do the people you work with, since program is not officially like your title, do they look at you
- 8:12as kind of like a wizard?
- 8:13A little bit. Yeah.
- 8:14Yeah.
- 8:14They do. And I mean, I've done one of the things that I've worked on was a forecasting tool to help
- 8:22us forecast our business performance, you know, anticipate what that's going to look like, which I
- 8:27think a lot of people did over the COVID times trying to forecast was extremely challenging. And they just
- 8:32call it, well, what does the Python tool say? So they don't really, you know, get what the underlying
- 8:38libraries are, what's going on, but they do associate my name with Python. And I don't know if they really
- 8:44understand what it all means. I certainly try and explain it, but at the end of the day, you know,
- 8:48happy with the results. And like you said, they do kind of think there's a little bit of a superpower,
- 8:52as you frequently say.
- 8:54That's fantastic.
- 8:54Or knowing that.
- 8:55Well, when you go and look at the basic things that a lot of the tools we're going to talk
- 9:00about, the frameworks that we're going to talk about result in, you could easily look over at
- 9:04something like Excel and go, well, six, one, half dozen of the other, that kind of the same,
- 9:09right? But then the amount of customization and specialization that if you go a little bit
- 9:14beyond just the, give me a histogram of this data, but you know, you dig into it a bit,
- 9:20it goes far beyond what things like Excel are able to do.
- 9:23Yes, absolutely. And it's funny you mentioned histogram, like even a few years ago, there wasn't
- 9:29even really a histogram function in Excel, you know, kind of one of the basic functions I use pretty
- 9:35much for any new data set is the histogram and Excel didn't have one out of the box. You could build one,
- 9:40but it wasn't there. And I think that just kind of speaks to Excel is approaching,
- 9:44approaching visualization from a very different perspective, I think, than the Python tools.
- 9:49Excel is much more of a, how can I quickly create something and kind of guide the user through it,
- 9:56and then give them almost infinite options to customize the visualization. So you can go in and
- 10:01tweak any individual data point or axes or colors, which, you know, is useful for getting started,
- 10:09but I don't think it scales very well. And it also doesn't have some of the more sophisticated,
- 10:14complex visualizations that you can do with the Python libraries that are out there.
- 10:18Like Sunburst and other amazing things. Exactly. Yeah, cool. All right. Well, let's mention your
- 10:24course real quick, because what we're going to cover today is inspired by the course. It's not the same
- 10:30thing as the course, but you recently published a course over in Talk Python, Python data visualization.
- 10:34This is really nice because the visualization landscape is so diverse and varied, and it's hard to pick,
- 10:42I think, what should I choose? How do I find something that, as you say, fits your brain? How do you find
- 10:48something that's modern and maybe is interactive or is good for publications? And so in this course,
- 10:53you know, you kind of just do a survey of many of the popular options. You want to give a quick
- 10:58elevator pitch on this and then we'll dive into the topics.
- 11:01Sure. Like you said, I think the Python landscape is, or Python visualization landscape is so rich. There
- 11:08are so many options and many people are discouraged and don't even know where to start. And I think when
- 11:14you try and marry up that landscape with the different types of problems you can solve with visualization,
- 11:20it really requires you to spend a little bit of time with each one of the main libraries or several of the
- 11:26main libraries to think about how they're going to solve your problem. And so the course steps through
- 11:32many of the common libraries gives you, like we were talking about, the amount of Python that you need
- 11:38to know to do visualization is fairly minimal. So we don't spend a lot of time on Python. It's more about
- 11:43the API and how to interact with each visualization library to use it in the way that it's intended.
- 11:51And then I also think it's important when people are thinking about visualization,
- 11:55it's not just about the library. It's also about thinking about visualization. And as I point out in
- 12:01the beginning of the course, visualization is a really broad topic. I mean, there are computer science
- 12:05classes that you can take a whole semester on. There are many, many books that are really strong.
- 12:11Edward, tough day comes to mind or tough. Yeah, exactly. Exactly. And when you start thinking
- 12:17about visualization that way, it changes the way you approach visualization from the Excel approach of
- 12:23how do I just build a bar chart to what is the information I have and how am I trying to convey it
- 12:29to the end audience? And so I spent a lot of time talking about that. And then I also think that there
- 12:37is a data manipulation component to this. Once you start to understand how to structure your data
- 12:44correctly using maybe not correctly, but most efficiently for data visualization. Once you start
- 12:50structuring your data in that tidy format, then it's very easy to iterate on your visualizations
- 12:56and zero in on what's going to work best for you and your end users. So that's what the course talks
- 13:02through. It talks to the concepts, real world examples of developing visualizations using many of the libraries
- 13:09we'll talk about and then how to customize those visualizations from very basics all the way up to
- 13:14building custom dashboards that can be highly interactive and potentially deployed for others to use.
- 13:21Cool. Yeah. I definitely learned a ton going through your course. It's over at talkpython.fm
- 13:25slash data viz. People can check that out. Let's get maybe a high level landscape. So view of the landscape
- 13:33before we dive into these topics, because there's different branches of this, I guess you would say.
- 13:39So there's a GitHub repo that you point out by Nicholas Rogier. I'm not sure how to say his name. Sorry, Nicholas.
- 13:46This is an adaptation of Jake VanderPlas' graphic about the landscape here. And so, you know,
- 13:53how's this picture fit for you? Do you think this is pretty accurate? Let me see if I can.
- 13:58I do, because I think it points out a couple different things when you start thinking about visualization.
- 14:03So one of the key things that you can
- 14:07glean from looking at this and for the people that are listening,
- 14:10it's a kind of starburst plot and
- 14:13you've got a whole bunch of linkages between these different visualization libraries.
- 14:18And something that jumps out is
- 14:20Matplotlib is at the center of many of these libraries
- 14:24and it's the foundational tool that's used to build other libraries.
- 14:29And so I think that's a key concept.
- 14:30You can use Matplotlib on its own
- 14:32or knowing Matplotlib makes it easier to use some of these other libraries that are built on top of it.
- 14:38And the other thing is not shown on this, but, you know,
- 14:42from a history perspective, Matplotlib is sort of the grandfather of all
- 14:46Python visualization libraries.
- 14:48It's been around a long time and what you see on the, for people that can see the visualization,
- 14:54on the left-hand side,
- 14:56JavaScript visualization is a little bit more modern, like you said,
- 15:01and what people expect when you think about an interactive visualization tool.
- 15:05And so that's a different approach for visualization that has some pluses and minuses.
- 15:09So you've got that Matplotlib and JavaScript,
- 15:12I think are the key distinctions for how libraries are constructed for visualization in Python.
- 15:17And there are some other ones, there are some other libraries,
- 15:20but the two that I really focus on are either Matplotlib based or JavaScript based.
- 15:26Sure.
- 15:26And the Matplotlib side, we have things like Matplotlib itself,
- 15:29but also Pandas and Seaborn and Scikitplot, GGplot,
- 15:34stuff that people may be familiar from there.
- 15:37And then on the JavaScript side, we've got Bokeh and Plotlib,
- 15:42some of the more, as you say, interactive ones.
- 15:44And D3JS is in there as sort of a foundational item as well.
- 15:48Exactly.
- 15:49Altair.
- 15:49I don't even see Altair in this list.
- 15:51Maybe it's in there somewhere, but it is.
- 15:53Yeah.
- 15:53It's over there hanging out close to Matplotlib.
- 15:56Exactly.
- 15:57Yes.
- 15:57More strongly related to JavaScript.
- 15:59Got it.
- 15:59The third one in the three main branches here is OpenGL.
- 16:04What do you know about the OpenGL ones?
- 16:07Yeah, it's a good question.
- 16:08I don't use them a whole lot.
- 16:09I don't have a whole lot of experience with them.
- 16:12I think there are certainly maybe certain like very high volume data analysis that might be
- 16:20where performance is really important, where some of those OpenGL libraries,
- 16:23I think were originally founded.
- 16:25But I get the sense that most people are gravitating towards either those Matplotlib or the JavaScript ones that we've talked about.
- 16:34I don't have as much experience nor see as much development there with those libraries.
- 16:38Yeah, maybe they're about visualizing changing data that is flowing in real time and you can actually see a change because, you know, OpenGL is basically a graphics library for animation, right?
- 16:48Sure.
- 16:48Yeah.
- 16:49I think that real time component is a good distinction.
- 16:51Yeah, probably.
- 16:53Now, before people run fleeing to the hills, just because some of these projects are grouped under JavaScript doesn't mean you have to write JavaScript to use them, right?
- 17:02Correct.
- 17:02Absolutely.
- 17:03Yeah.
- 17:03And all the ones that I cover and the ones on here actually have a really nice API on top of it.
- 17:10The JavaScript is abstracted away.
- 17:12And in some ways, there's some benefits, like I'll call it Altair because it leverages VegaLite.
- 17:19And anytime that underlying JavaScript library is updated, you kind of get all of those benefits for free through Altair.
- 17:27So it's, you know, in the spirit of open source, building on the shoulders of others.
- 17:32And there's a lot of benefits to having that JavaScript foundation.
- 17:35And you don't need to understand JavaScript to use any of them.
- 17:40Yeah.
- 17:40And Altair really is like a transformation layer into a VegaLite definition, which is then processed by JavaScript to be rendered.
- 17:48So there's even this sort of separation layer.
- 17:51So it's not like you necessarily have to change your code to pick up the changes there.
- 17:55No.
- 17:55And I think at some point, depending on how deep down the rabbit hole you go, there could be points where, okay, it is really,
- 18:01if you're doing something highly custom or something really unique, understanding what's going on under the hood could be useful.
- 18:07But you can get pretty far without having to know that.
- 18:11Maybe you're trying to make a book or an article and you need something just so you're like, you know what, I'm just going to dump out the VegaLite definition and just add two things to it, but create it through Altair.
- 18:21Exactly.
- 18:25This portion of Talk Python To Me is brought to you by Microsoft for Startups Founders Hub.
- 18:29Starting a business is hard.
- 18:31By some estimates, over 90% of startups will go out of business in just their first year.
- 18:36With that in mind, Microsoft for Startups set out to understand what startups need to be successful and to create a digital platform to help them overcome those challenges.
- 18:46Microsoft for Startups Founders Hub was born.
- 18:48Founders Hub provides all founders at any stage with free resources to solve their startup challenges.
- 18:55The platform provides technology benefits, access to expert guidance and skilled resources, mentorship and networking connections, and much more.
- 19:04Unlike others in the industry, Microsoft for Startups Founders Hub doesn't require startups to be investor-backed or third-party validated to participate.
- 19:13Founders Hub is truly open to all.
- 19:16So what do you get if you join them?
- 19:17You speed up your development with free access to GitHub and Microsoft Cloud computing resources and the ability to unlock more credits over time.
- 19:26To help your startup innovate, Founders Hub is partnering with innovative companies like OpenAI, a global leader in AI research and development, to provide exclusive benefits and discounts.
- 19:35Through Microsoft for Startups Founders Hub, becoming a founder is no longer about who you know.
- 19:41You'll have access to their mentorship network, giving you a pool of hundreds of mentors across a range of disciplines and areas like idea validation, fundraising, management and coaching, sales and marketing, as well as specific technical stress points.
- 19:54You'll be able to book a one-on-one meeting with the mentors, many of whom are former founders themselves.
- 19:59Make your idea a reality today with the critical support you'll get from Founders Hub.
- 20:05To join the program, just visit talkpython.fm/foundershub, all one word, no links in your show notes.
- 20:11Thank you to Microsoft for supporting the show.
- 20:13Well, let's start with the granddaddy, as you put it, Matt Plotlib.
- 20:20So, there's so many shiny new things, but you make the case that knowing Matt Plotlib is still really worthwhile and really important, right?
- 20:29Because it is the foundation of so many things.
- 20:31Exactly.
- 20:31It is the foundation, and it's so important to know it.
- 20:35And also, it is extremely highly customizable.
- 20:40And I was thinking about this, and in some ways, I think if Matt Plotlib, if you stripped out a lot of the old and like everything that they've done in the last three to five years and just focused on that new, and if you could erase all the old tutorials and all the maybe ancient answers on Stack Overflow and just focused on the new stuff, people would have a much different perspective on Matt Plotlib.
- 21:06I think it's hard when you have something that's been around so long and has evolved that you do get some cases out there where, okay, an API or the way they approached a visualization in the past was clunky.
- 21:19But in the five or 10 years since, it's improved.
- 21:23And if you use the new and improved, it's really streamlined and really easy.
- 21:26So I think it's important that people not get turned away right off the bat from Matt Plotlib.
- 21:32And there are certain types of visualizations where if you need a high degree of customization, if you want to print it out or include in a manuscript or a book, Matt Plotlib is really useful and powerful for it.
- 21:47In that distinction about the new and the old, originally when Matt Plotlib came out, correct me if I'm wrong, it's my limited understanding, is it was somewhat modeled on the Matlab way of programming and it had this imperative API.
- 22:00The new one is more object oriented, isn't it?
- 22:03Yes, that's exactly right.
- 22:04So there's this state based interface that was based on Matlab and for people making that transition from Matlab to Matplotlib, it was seamless, right?
- 22:14And they really understood it and made sense.
- 22:15But that way of doing things doesn't, it's not really Pythonic.
- 22:20And so the object oriented interface is newer and is clearly the direction that Matplotlib documentation wants to steer you down that path.
- 22:30And if you stay on that path, then it makes more sense, I think, from a Python perspective, and you do have a tremendous amount of power.
- 22:38And I would say the other thing that I think turns people off with Matplotlib in the past is the visualizations are relatively unstyled.
- 22:47I mean, they're kind of plain, whereas some of these newer libraries just out of the box make something that look really nice.
- 22:54Matplotlib allows you to customize it, but that's extra work.
- 22:58One of the things that Matplotlib has done is they have a new theming or a relatively new theming API.
- 23:04And so if you use that, then you do get visualizations that look a little nicer out of the box and are more visually appealing.
- 23:12Yeah, it does have that kind of, it looks fine, but it kind of just looks a little bland, right?
- 23:17And it doesn't have that D3JS feel.
- 23:19Absolutely.
- 23:19So I got to give them some pretty mad props on the XKCD.
- 23:23Exactly.
- 23:24Have you, you've seen this, it sounds like.
- 23:28Yes.
- 23:28Yes, I have.
- 23:30So if people, I'm sure most people out there listening know the XKCD comic, right?
- 23:34If you don't go to your terminal or command prompt and run Python 3 and then just type import anti-gravity, then you'll know.
- 23:41But they have this, it's been around forever and it has this sort of style of like handwritten, but not handwritten.
- 23:47And one of the themes you can get is you can get the XKCD theme.
- 23:52Exactly.
- 23:53Yes.
- 23:54And it's really cool.
- 23:55And every once in a while, you'll find an article that someone put together where they show this beautiful visualization.
- 24:02And then you'd be surprised that it's matplotlib and they show all the steps and you can configure it and you can make something as nice as any of the JavaScript frameworks that are out there.
- 24:13But it does take some time.
- 24:14There are more lines of code to get there.
- 24:16Yeah, that's true.
- 24:17On this XKCD thing, it might sound like it's, well, that's funny.
- 24:20Like everybody loves XKCD.
- 24:21I do think there is some value to presenting results, whether that be a user interface or a visualization of analysis where you want to give it this preliminary look, this unfinished look.
- 24:34Right.
- 24:34And so if you're going to come into a meeting and you want to say, this is what the early data says.
- 24:39This is what our first pass analysis says.
- 24:42You know, put it in the XKCD.
- 24:44They might set that tone versus if it's like perfect and beautiful.
- 24:47Like, well, you're done.
- 24:48Like, no, no, we're not done.
- 24:49We're not.
- 24:49We're really far from done.
- 24:51This is just the beginning.
- 24:52I just want to give you a hint of what we're finding out.
- 24:55Right.
- 24:55I think there's a way that you could actually use this that would be practical.
- 24:58I agree.
- 24:59I mean, it's a good point.
- 25:00And, you know, one of the things that I think when you go into a business setting, everybody's used to standard Excel plots.
- 25:07And when you bring in something else like this, like an XKCD plot or some other plot that people aren't used to, it does get them to focus and look at it a little bit different and can steer the discussion in a little bit different way.
- 25:21Yeah.
- 25:21So one of the little areas that I thought was just really nice and really simple would be things like if you go and plot something with matplotlib and you have a lot of ticks along the bottom, it's very common that the words start to overlap each other.
- 25:38And you're like, well, this isn't working.
- 25:40And just little simple things like putting an angle on the values and the X axis can make it so much nicer.
- 25:48Right.
- 25:49Yes.
- 25:49Yeah.
- 25:49So.
- 25:50And one of the things that I do have a soft spot in my heart from matplotlib on the official documentation, I think under the tutorials, they took one of the blog posts that I wrote on matplotlib and it has been incorporated into the official tutorial.
- 26:06Oh, that's cool.
- 26:06About how to get started.
- 26:07So I think that's kind of cool.
- 26:09You know, I.
- 26:09Yeah.
- 26:10Proud of that.
- 26:10Absolutely.
- 26:11Another one that I thought was nice to know about that's not at all obvious is formatters using like F string formatter type things for when the data gets put up there.
- 26:22So maybe instead of having, you know, two, zero, one, zero dot, you know, some huge long decimal, you could format that as a number with no decimal point.
- 26:32No sense.
- 26:32Right.
- 26:33I use that all the time for currency.
- 26:35So if you're showing like millions, you don't want to have all the zeros.
- 26:39Maybe you just format as a dollar sign to M or whatever.
- 26:43Yeah.
- 26:43And matplotlib makes that very easy to do.
- 26:46And dates here, as you're showing are always one of those challenges with it.
- 26:50With any time you're plotting something, trying to figure out the right level of dates that convey the information, but don't crowd out the visualization.
- 26:57Yeah, absolutely.
- 26:59So there's a lot to learn there.
- 27:00But I still think a lot of people will be doing matplotlib, but also a lot of people will be choosing the newer ones.
- 27:06Yeah.
- 27:06Now for each of these, you came up with some pros and cons.
- 27:10So for the pros category of matplotlib, you have robust options that can do almost anything and lots of documentation examples.
- 27:18Yeah.
- 27:18Yeah.
- 27:19I mean, that is anything you want to do.
- 27:22You can do it in matplotlib.
- 27:24Yeah.
- 27:24The challenge is going to be, and you can find documentation examples.
- 27:28What does get challenging is because the API has evolved and changed over time, looking at the official docs are great.
- 27:36But when you can't find it there and you go searching on the web, you will find a tutorial from seven years ago.
- 27:42And the way they're telling you to do it may work, but it's not the most efficient way or may not work well with the way the rest of your code is structured.
- 27:50Right.
- 27:50Maybe it's not taking advantage of the themes or maybe it's using the stateful API instead.
- 27:55Exactly.
- 27:55There's a lot of reasons it could be.
- 27:56Exactly.
- 27:57The final, you know, challenge with matplotlib is there is some degree of interactivity, but it's not at the high level that some of the other libraries have.
- 28:07So if you have a scatter plot and you want to zoom in on an individual plot, look at the data, it's not as easy to do in matplotlib as it is in some of the other libraries.
- 28:17The other pro that I'd say with matplotlib is if you are trying to get your visualization in another format.
- 28:24So you're trying to put it in a document, you're trying to put it in a PDF, SVG, you know, any kind of graphic format.
- 28:30Matplotlib supports that out of the box.
- 28:33It's very easy to save it in any format and move it into whatever other document you have.
- 28:39Whereas some of the other ones are a little more challenging to do that or maybe don't have as high quality output as matplotlib does.
- 28:46Especially if it's an SVG, you can scale that almost infinitely, right?
- 28:50Yes, exactly.
- 28:51Yeah.
- 28:52Another thing that is going to be a repeated theme throughout many of these frameworks, but it's also here, is the ability, you know, not to just make one chart or one picture or one plot of some variation, but to build multiple plots.
- 29:08Right.
- 29:09So for example, a matplotlib, you can put, there's a, on the examples here, they have a MRI with EEG.
- 29:15And so they've got the brain, a picture of the brain and then a MRI, and then also some other measurement, which I don't know enough brain science to know what it's for, but.
- 29:25Neither do I.
- 29:26It looks cool.
- 29:26It looks cool.
- 29:27Right.
- 29:28But you can, you can create a single picture that puts like two graphs and then a graph below it.
- 29:33Right.
- 29:33There's a way to compose these beyond just making one picture.
- 29:36Exactly.
- 29:37And if you think, well, I could do this in Excel, I could put two or three graphs next to each other.
- 29:42But here's where the power of matplotlib comes into play.
- 29:44Let's say you were running experience experiments in a lab and you're doing hundreds of these.
- 29:49You wouldn't go into Excel and individually position all this.
- 29:52No, you write your Python script.
- 29:54You develop that layout for those different visualizations, and then you're done.
- 29:59You know, once you get the data, you just kind of run through it and you can iterate through it.
- 30:02And that's really the power using a real programming language, do visualization versus just trying to do one-off visualizations in Excel or some of the other tools out there.
- 30:12Sure.
- 30:12And instead of necessarily putting those pictures into a notebook output, you could write a loop.
- 30:17It says, go get me this experiment's data and save, generate this graph and save it to a file.
- 30:22Get the next one.
- 30:23You know, find out even what's in the folder, pull them all in, loop over them, generate one picture, then the next, then the next, then the next with the right name.
- 30:30And so it could just be all automatic, either in a script or just in a notebook that doesn't have as much output.
- 30:36Right.
- 30:36Exactly.
- 30:37Yep.
- 30:37The full power of Python is at your fingertips.
- 30:39Yeah, absolutely.
- 30:40All right.
- 30:42So that's the kernel of one half of this branch.
- 30:46Maybe the next one.
- 30:47This one's a little bit surprising to me because I just don't do enough pandas.
- 30:51But pandas, when I think about pandas, it's about manipulating data and reading data and transforming data and doing that tidy data preparation that you spoke about.
- 30:59All those different things.
- 31:00But there's also graphing built into pandas itself.
- 31:04Yes.
- 31:04And that graphing is built on top of Matplotlib.
- 31:07So that's why from a course perspective and thinking about visualization, I think having that Matplotlib foundation sets you up so that when you're in pandas, where, like you said, you'll be doing the majority of your data input manipulation and analysis.
- 31:23It's important to understand what visualization tools you have there.
- 31:28And a lot of the standard visualizations that you want to do with a line chart, scatter plots, bar charts, box plots, histograms, you can do with pandas.
- 31:38And it's mostly a very thin wrapper around Matplotlib.
- 31:43So that's why it helps if you get this output and you look at it and say, well, I just want to customize it a little bit.
- 31:49Typically, if you know that Matplotlib API, then you could customize it in pandas or go into Matplotlib and customize the pandas output.
- 31:59So I think that is really useful.
- 32:02The other thing that's interesting about pandas is it will allow you to plug in other backends.
- 32:09So Matplotlib is the default, but you can enable backends for Plotly and a few of the other visualizations.
- 32:16So it can be kind of this universal interface to visualization.
- 32:20In my experience, I don't use the pandas visualization a whole lot, but I do think it's important for people to understand it's out there because there are times where that's the quickest way to get something out there.
- 32:33And it's sufficient for the quick and dirty needs.
- 32:37Well, yeah, sometimes you open up a panda, you read something like a CSV or whatever, and you just want to know, well, what is this?
- 32:43And you would type DF for data frame, DF dot head or tail.
- 32:47And it gives you just a little brief view into it.
- 32:50There's the picture's worth a thousand words sort of thing.
- 32:53And if all you have to say is just dot plot and that's it or dot hist.
- 32:58And now you have a picture instead of that, that tail or head equivalent.
- 33:02That's a really cool way to just sort of quickly explore the data.
- 33:06Exactly.
- 33:07And there are some other more unique plots in pandas.
- 33:12So there are some plots called the Andrews curves and parallel coordinates that are more advanced.
- 33:20And to be honest, you know, it's more of a data science kind of machine learning plot.
- 33:26It's not something you use that often, but it is important to understand if you need it, that it is out there in pandas for you.
- 33:35Yeah.
- 33:35The Andrews curves reminds me a lot of like a Lorentz generator, a tractor.
- 33:40Something from chaos theory.
- 33:42A bunch of lines like looping over and over and over and over.
- 33:46Exactly.
- 33:46It looks pretty cool.
- 33:47It does.
- 33:48You'd probably blow some people's minds if you put that up.
- 33:50But, you know, it's specialized.
- 33:51But it does speak to there when you start getting into this visualization world, there are specialized libraries.
- 33:59And you might find something in your niche that you're working that, you know, this is really useful and it's really powerful.
- 34:05And it's maybe hard to do in other tools.
- 34:07And boom, it's easy in pandas or some of the other tools.
- 34:10Yeah.
- 34:11So let me tell, I normally try to avoid saying code on here because it's audio, but given a data frame, I can say plot.figure and then just call Andrews curves, give it the data frame and a name and boom, you get this amazing visualization of your data.
- 34:27Like on three incredibly simple lines.
- 34:29Yes.
- 34:29And this is the kind of stuff that I was referring to at the beginning when I said like, you almost don't need to know Python.
- 34:34I mean, technically, there's a little bit of Python, but barely, right?
- 34:37Right.
- 34:38And it's just starting to understand how to think about how you could use these tools and getting familiar with it so that the first time you're trying to build something is not you're not also trying to learn in the API and all these other visualization topics on top of trying to solve whatever problem you're trying to solve with visualization.
- 34:55Well, yeah, there's definitely some neat stuff to do visually with pandas and people should certainly be using it.
- 35:02Also, based on the Matplotlib kernel, we have Seaborn.
- 35:07Where does this fit into the world?
- 35:08So Seaborn builds on top of Matplotlib, like you said, but it really focuses more on doing statistical analysis of your data and taking that data and frequently transforming it in some ways and developing a visualization.
- 35:28So I use it a lot for histograms and box plots.
- 35:32So I use it a lot for the time to see it a lot of people who have seen the data and see it a lot of people who have seen the data and see it a lot of people who have seen it in some ways.
- 35:37So that's where you take a lot of people who have seen it in some ways.
- 35:43And that's where you take a lot of people who have seen it in some ways.
- 35:47So you have seen it in some ways.
- 35:52So you've got nine, 12, you know, however many plots you need.
- 35:55And it gives you an opportunity to spot data anomalies, trends very easily and present a wealth of information in a very compact frame.
- 36:06And what I like about Seaborn is it is very easy to do this.
- 36:11So the code that's required to do this is typically, you know, one or two lines of code.
- 36:16And you get this really nice plot that has different colors.
- 36:20It has data varying across the rows and columns.
- 36:23You can change the shape, the size, everything to, you know, with those visualization concepts that we cover in the beginning to develop visualization that really give you a lot of insight very quickly.
- 36:36Yeah, absolutely.
- 36:37It's very statistical focused, isn't it?
- 36:39It is.
- 36:40It is.
- 36:40You know, it is very statistical focused.
- 36:43And some of the plots are more complex and having a statistical background will help you understand them.
- 36:49But simple things like just doing bar plots or histograms or account plot or a heat map, you know, are relatively straightforward to explain to others and easy to create with Seaborn.
- 37:02And then you're right.
- 37:03There are some of the plots that are definitely much more kind of deep in the statistical toolbox.
- 37:08And you have to know how you would explain them to others.
- 37:12So this faceting thing is pretty interesting.
- 37:15If you've got, if you go to the Seaborn examples, they've got one for looks like five different variables or sorry, four variables.
- 37:24And they're looking at two different groups on one of them.
- 37:26And you can just say, show me, you know, basically instead of give me a picture of this data, take any two pieces of information and generate a graph to show me how those things correlate.
- 37:38One of the pieces of data that you did a lot of work with was the automobile gasoline efficiency data from the EPA, right?
- 37:45Yes.
- 37:46What I find really fascinating about this is when you get the data set up properly and you want to look at all these different relationships, it's very easy to slice and dice and mix up those relationships to see where the trends are.
- 38:02So if we're looking at fuel efficiency, we could look at the year the cars were manufactured.
- 38:08We could look at it by are they front wheel drive, all wheel drive cars?
- 38:12How many cylinders do they have?
- 38:14Are they electric?
- 38:15SUV versus SUV?
- 38:16All these things.
- 38:17Yeah.
- 38:18Yeah.
- 38:18You know, there could be a price component.
- 38:20There could be just a whole bunch of different ways to look at it.
- 38:24And when you have a big data set, those are the types of things that are very difficult to do just by looking at the numbers.
- 38:31And that's where visualization really shines.
- 38:33And where Seaborn makes it tremendously easy to just quickly iterate through and say, okay, I want to look at these two variables together.
- 38:41Now let's layer in a third variable.
- 38:42Now there's a fourth variable.
- 38:44Well, I don't like this.
- 38:45Let's switch them around a different way.
- 38:46And I use Seaborn a lot because of that flexibility of just exploring the data, quickly figuring out what those trends are, what those insights are, and rapidly iterating through it for the exploratory analysis.
- 39:01Another thing that Seaborn has is it looks really nice and it has this idea of themes.
- 39:07Yes.
- 39:07So it's easy to make it look good, right?
- 39:09It is.
- 39:10I mean, Seaborn out of the box applies some themes and also does things behind the scenes with the visualization to make it cleaner and to try and format the data so that, you know, dates line up appropriately and colors look nice.
- 39:26And there's appropriate spacing and things like that.
- 39:30And there are some other things that are pretty easy to control to turn on and off or change the color palettes with Seaborn.
- 39:38But generally out of the box, it strives to look good and it looks nice.
- 39:43And what's also beneficial about it is it is just matplotlib under the hood.
- 39:47And so if you get to the point where you've done your analysis and things look pretty good, but you want to do some tweaks, there are some convenience functions in Seaborn to do that.
- 39:57But there's also, it's just matplotlib under the hood.
- 40:00So if you know matplotlib and you want to tweak some things, you can do that as well.
- 40:04Yeah, for sure.
- 40:05One thing I want to maybe take a step back on here with matplotlib on a lot of these examples, not this one I got up here, but lots of them.
- 40:14I'll see, and you talked about this, that we'll have like semicolons some of the time.
- 40:20And, you know, Python's famous for not requiring semicolons at the end of lines.
- 40:25What's the story there?
- 40:27Yeah, the semicolons are just an artifact of when you're developing in a Jupyter notebook.
- 40:32And when you show that plot, that matplotlib will show additional information about the plot.
- 40:39So it will have kind of like a string descriptor, and then it will show the plot.
- 40:44And if you use the semicolon, it will suppress that extra information.
- 40:48So all you see is the plot.
- 40:49So it's certainly, it's not required by any means.
- 40:53And you're right, it does.
- 40:55For people that have played with Python for a while, you kind of wonder why there's semicolon there.
- 40:59But it's just to suppress some of that extra information that gets shown.
- 41:04And it's not on all the lines.
- 41:05It's just on certain plotting lines.
- 41:07The other lines don't need it, right?
- 41:08So it's a little unclear if you're not sure, which is why.
- 41:11All right.
- 41:12Moving on from Seaborn, we start to bridge our way over into the JavaScript D3JS side of things with Altair.
- 41:22And Altair, I think, is certainly very well known.
- 41:25Very well respected.
- 41:26It's one of the newer ones, isn't it?
- 41:27It is.
- 41:28Yes.
- 41:28I have to look.
- 41:29I don't remember off the top of my head.
- 41:30But if Matplotlib was started in like 2012, Altair is probably in the last five years or so.
- 41:39Yeah.
- 41:40Yeah.
- 41:40I would imagine.
- 41:41So definitely much newer, but has been tremendous amount of updates.
- 41:47What was it?
- 41:482015.
- 41:49Okay.
- 41:50Yeah.
- 41:50And Jake, who started it and maintains it, did an awesome job of leveraging a lot of the best practices from Python libraries as well as R to build Altair.
- 42:02And like we said, it's built on top of Vega.
- 42:04So there's that, you know, anytime that new work is done in that JavaScript library, it's easier to port it to Python so that you can leverage that as well.
- 42:15Quite popular, almost 8,000 GitHub stars.
- 42:18And Jake Vander Plaas added or changed 353,000 lines and removed 240,000 lines.
- 42:25And Allison BG as well, something on a similar scale.
- 42:29That's a ton of work.
- 42:30Yeah.
- 42:31Yeah.
- 42:31It's a fabulous library.
- 42:33I mean, there's one of the things I really like about the library is the gallery.
- 42:37Yeah, that's where we're going now.
- 42:38And the documentation is really great for Altair.
- 42:42And once you start to get into it, you need to spend a little bit of time to just make sure you understand how the library works.
- 42:48But then probably 90% of the time, you're going to go to the gallery and try and find something.
- 42:53And you look at the code and you're like, oh, okay, that's how I do it.
- 42:55Yeah.
- 42:55Like, this is the one I want.
- 42:56Yes, exactly.
- 42:58There's a lot of interesting aspects about creating graphs here.
- 43:01So one of the common ones is to create a scatter plot, which is called mark circle in this world, right?
- 43:07And hopefully those are similar enough to be put together.
- 43:11It's the same thing.
- 43:12And those create little dots that show like, where's all the data from these different categories, say.
- 43:17And one thing that's interesting is you can say, I'd like to color.
- 43:21I want the X to be this value and the Y to be that value.
- 43:25But then I want the color of the dot to be based on another column and maybe the size to be on a third one.
- 43:32So in this EPA car data, you could say, well, I want the color of the dot to be the type of vehicle, like an SUV or a car or whatever.
- 43:41And then I want the size to be the number of cylinders in the engine.
- 43:45And that's just incredibly easy.
- 43:47But it really draws out the data.
- 43:49It does.
- 43:49And Altair makes it easy to combine this in different ways.
- 43:54So if you want to have a scatter plot and bar chart or a histogram, you can combine these together.
- 44:01You can also do the faceting that we talked about with Seaborn.
- 44:04You can do with Altair, where you change the variable across the columns and rows to get different plots.
- 44:11And the other thing that Altair, one of the other things Altair introduces is interactivity out of the box.
- 44:19So you, because it is JavaScript based, you then have that ability to go in.
- 44:25And as you're doing here, for the people who can see it, you can hover over a spot and then control what information is shown for that hover.
- 44:34So you can see, oh, what's going on with this individual dot?
- 44:38Well, here's the, it's a Volkswagen Rabbit from Europe and it has a 71 horsepower engine and gets 31.9 miles per gallon.
- 44:45And so it's really cool and really useful for doing that exploratory analysis where you kind of want to see the individual data points and maybe drill into it a little bit more.
- 44:55Yeah, the interactivity is great.
- 44:57The ability to add custom tooltips and then have those tooltips, have like F-string style formatting as well.
- 45:04It's pretty excellent.
- 45:05So if the data doesn't show up just the way you would like, you know, you can have something.
- 45:09You can say small, medium, large versus like this number or that number.
- 45:13So you can kind of think about it separately and differently, right?
- 45:16Exactly.
- 45:16And one of the other things that's interesting about Altair is it does try to infer different aspects about your data.
- 45:26So it tries to understand, well, is this data continuous data?
- 45:30Is it date data?
- 45:31And you can specify the different types of data.
- 45:35So you can say that it's quantitative or it's an ordinal value or nominal value.
- 45:41And the actual visualization will change a little bit depending on that data type.
- 45:46That's a unique thing that Altair does.
- 45:48And I think that's one of those things that as you start going down this visualization path, you start to think about your data a little bit differently and think about how you want to present it.
- 45:58And Altair gives you that window into all the flexibility you have with presenting data.
- 46:03One of the really nice aspects of interactivity of these is the legend.
- 46:08So the legend will show, you know, it really looks nice.
- 46:11It matches like the color and the name and it's in a font that is pretty readable.
- 46:15But if you set it up right, you can go and actually click on these and either just highlight one or you can have them sort of be toggle buttons.
- 46:23And so if you want to just focus on, you know, let me pull out just in this case, it's got like agricultural finance, government type of spending or something.
- 46:31You can just say, I want to just see the educational and health and click that and it highlights that sort of separate from the rest of them.
- 46:37Yes.
- 46:38And it's really easy.
- 46:39There's like one line of code that you need to do to set that all up.
- 46:42Add selection or something simple like that, right?
- 46:45Exactly.
- 46:45Yep.
- 46:46One other thing I want to touch on with Altair here, and I also want to talk about an example, but you talked about, was it data?
- 46:54Was it transforms or something?
- 46:56You've got to either use the file or a little server or something to process the data.
- 47:01What's the story of that?
- 47:03Yeah.
- 47:03So one of the things that can be a little tricky with Altair is behind the scenes, it's translating whatever data you have into a JSON file.
- 47:13And so what that means is when you have 10 or 20 data elements, it's not that big a deal.
- 47:18But when you have thousands of elements, you can suddenly get to a point where that file is really huge.
- 47:24And so rightly so, Altair makes sure that you don't inadvertently embed that in your notebook.
- 47:32So you could end up with your Jupyter notebook suddenly being, you know, 50 megs because you've got all these Altair visualizations in there.
- 47:40So there are some options for how you can manage that data so that it's not necessarily stored directly in the notebook file.
- 47:49Maybe you have that data stored separately, kind of like on a cache file in a directory or potentially more of like a real-time streaming option where you have a backend service that's running behind the scenes that streams up the data to you.
- 48:03So there is a little bit more complexity sometimes with Altair to get it running.
- 48:08And because of that visualization, because of the JSON approach that it uses.
- 48:14So that's certainly one of the watchouts and things to keep in mind.
- 48:19The other thing that sometimes has been a challenge for me with Altair is saving visualizations.
- 48:24So if you want to create something as an SVG in Matplotlib or Seaborn, it's very straightforward.
- 48:30You just save it.
- 48:32With Altair, sometimes it can be a little challenging because of the way it's trying to render those visualizations and save them to a PNG or SVG file.
- 48:41Yeah, so you've got to set that up and select the right one.
- 48:44It'll work if you don't have too much data without doing that, right?
- 48:46But then there's some limit where it's like, you know, this is too much.
- 48:50You've got to have to push it outside of the notebook.
- 48:52Yes.
- 48:53You'll get an error message and it'll kind of tell you what's going on.
- 48:56But I do mention it in the course, you know, some of the options for getting around it.
- 49:01And the documentation is good about what those options are as well.
- 49:04Sure.
- 49:04All right.
- 49:05So to wrap up Altair, I want to talk through a little example here.
- 49:09And I'll put this example in the show notes so people can check it out.
- 49:14Let's try to describe this picture here.
- 49:16And like I said, I'll put it in the show notes so people can see it.
- 49:20You have this Amazon author reviews for the top 20 most reviewed books,
- 49:25most reviewed authors over the last 10 years or something like that, right?
- 49:29Yes.
- 49:29Tell us what's going on in this picture.
- 49:30And we can maybe talk through just the API components that make it happen.
- 49:34Sure.
- 49:34So behind the scenes, the data is, I can't even remember, well, I guess this one's through
- 49:392016 or so, 10 years or so of Amazon reviews.
- 49:44So on the X axis, it's 2009 through 2020.
- 49:50And then on the Y axis, we have authors.
- 49:52So a lot of famous authors in this timeframe.
- 49:55And some of these authors will have one book a year.
- 49:59They'll have multiple books a year.
- 50:00And so it will have a circle where for each author for the year they published the book
- 50:06and the size of the circle represents how many reviews they had.
- 50:11So this is a really quick way to see how consistent some of these top authors are over years.
- 50:17You know, it's sort of interesting, like Dale Carnegie is at the top.
- 50:21I mean, he wrote this book, I don't know, probably 50 plus years ago, but it's still a bestseller
- 50:27on Amazon.
- 50:28And then you have other people that are maybe a little more sporadic, but it's a very easy
- 50:35way to see the consistency and then the number of reviews that each author gets for a year.
- 50:40And I would say, you know, if an author has more than one book, obviously they'll have more
- 50:45reviews.
- 50:46So it's not broken out by book.
- 50:47It's just purely by author.
- 50:48Right.
- 50:48And when you build up this picture, some of the things that happen is there's a legend that
- 50:52shows up.
- 50:52The legend is basically just a copy slightly offset of what's on the left.
- 50:56But what you really want to know is what's the size of the circle means.
- 50:59So you can add like an alternative legend and you can put a grid behind it and make it
- 51:04really easy to follow the timeline.
- 51:06There's just a really cool bunch of features.
- 51:08And this is the kind of picture I was thinking about when we were thinking, when I said, you
- 51:13know, like if you just go and call plot or circle or whatever, you know, mark circle, you
- 51:19end up with something that's not all that impressive.
- 51:21But if you layer a few of these ideas on, then it's great.
- 51:23We said we've got the author in the year and then the size, we configure the size based
- 51:29on the number of reviews.
- 51:30Then we configure the color based on the author so that it's a little easy to follow the
- 51:34little easier visually to look at this and see the information.
- 51:37And then we also do this thing that is interesting with Altair is when you think about Seaborn
- 51:45and Matplotlib and Pandas and some of the other libraries we'll talk about, you typically do
- 51:51the data manipulation in Pandas.
- 51:53Altair has its own ability to do manipulation and transformation of data.
- 51:59And so there is this option, you know, I could have filtered it down to the top 20 customers
- 52:04or authors, excuse me.
- 52:06But I use this transform filter to select only the top authors.
- 52:12And that's all in Altair, not using any Pandas.
- 52:16And then the final thing that we do is configure the width and the height and the title.
- 52:21So that's all, you know, kind of one long piece of code that looks intimidating as you, you know,
- 52:28maybe if you haven't worked with Altair.
- 52:29But when you take a step back and break it down and think about what it is you're trying
- 52:34to do with your visualization.
- 52:35And then with the basic understanding of the Altair API, it's pretty straightforward and extremely
- 52:41powerful.
- 52:42Sure.
- 52:42You kind of got to do it in steps.
- 52:44It's a very fluent API, you know, Alt.chart.markcircle.encode.configure.
- 52:51But if you take each one of those relatively simple function calls, then you try to understand
- 52:57that and see what you're doing.
- 52:58And then it turns out to be not too bad.
- 52:59Yeah, exactly.
- 53:00And what people need to realize when they're thinking about this course is anytime I develop
- 53:06code and it looks like this and it has this many lines, you're right.
- 53:08It wasn't, I didn't start off.
- 53:10I did first, let's just do author versus year.
- 53:12I don't like this.
- 53:13I need to tweak one more thing.
- 53:14Then I need to tweak one more thing.
- 53:16And you iterate over it to get there.
- 53:18And once you kind of understand how all these libraries work, it's not too much work, but
- 53:24it does take a little bit of time.
- 53:25And that's why it's important to, you know, dive into the data and play with it and experiment
- 53:30with it and see what works for you.
- 53:32Yeah, it almost is its own little mini language.
- 53:34It is.
- 53:36It is.
- 53:36All right.
- 53:37Before we move on to the next one, question from the audience.
- 53:40I'm all out there.
- 53:40Can we do responsive and animated workflow diagrams with matplotlib?
- 53:44For example, continuous builds development on different server or deployment on different
- 53:49servers?
- 53:49Not entirely sure exactly what you're asking, but certainly you can automate these things,
- 53:55right?
- 53:55It doesn't have to be in a notebook.
- 53:56This could all be put into a script, right?
- 53:59Yes.
- 53:59And there are matplotlib does support some animation.
- 54:03And I can't remember how much of this is out of the box matplotlib versus third party libraries.
- 54:09But I've certainly seen visualizations that people have done with matplotlib where it's, you know,
- 54:13something changing over time or steps in a process.
- 54:16You can do that with matplotlib.
- 54:19So you could create one of those language battles.
- 54:22Have you ever seen those where like over 20 or 30 years, it's either a browser or the language
- 54:27is when the book is popular, then it goes up.
- 54:29And yeah.
- 54:29Yes.
- 54:29Yeah.
- 54:30Yeah.
- 54:30You could do that.
- 54:31I'd have to look and see what would be the best approach, but those sorts of options are
- 54:36out there.
- 54:36Fun.
- 54:37All right.
- 54:37Sticking in the JavaScript side of things, the other really popular one over there is Plotly.
- 54:42What's special and unique about Plotly?
- 54:44I think Plotly is special and unique because it is a newer plotting library, kind of on the
- 54:52order of Altair.
- 54:54It is supported by a company out of Canada, but the Plotly visualization library is completely
- 55:04open source.
- 55:04It is based on JavaScript.
- 55:06What I like about it is everything is interactive out of the box.
- 55:12So any plot you make, as soon as it renders, you can take your mouse and you can hover over
- 55:17it.
- 55:17You can zoom in.
- 55:19You can limit the range of data.
- 55:22And so that is really powerful.
- 55:24And then the second thing that I really like about it is the history of Plotly.
- 55:29There was a separate Plotly visualization.
- 55:32And then there was something called Plotly Express, which was streamlined.
- 55:36And the Plotly Express API, in my opinion, is very similar to Seaborn.
- 55:42And it is very, I think it's Pythonic.
- 55:45It's very easy to understand and pick up.
- 55:47And over time, they've expanded it to where now that's kind of the default visualization.
- 55:53So it's very easy to get started with Plotly.
- 55:56It makes those interactive plots out of the gate.
- 56:00And then it does have some unique plots.
- 56:03So some of the kind of custom tree map plots, scatter matrix.
- 56:07You can do plotting on maps to show geographic plots.
- 56:12Those are all like out of the box, work pretty well and are fairly simple to create.
- 56:20Yeah.
- 56:20The tree map and the sunburst, those come from Plotly, right?
- 56:23Yes.
- 56:24At least are in Plotly is one of those things.
- 56:27And one of the other things that is interesting about Plotly, like any of these visualization
- 56:33tools, you have to be able to go in and configure and customize and tweak things.
- 56:39And Plotly gives you that ability to generate a plot, but then you can update it over time.
- 56:45So you can change colors.
- 56:47You can change the way data is presented.
- 56:49You can change pretty much anything with the plot using a fairly simple API as well.
- 56:55Yeah.
- 56:55People should definitely go and check out the tree maps example.
- 56:59One of the really cool interactions is, for example, the one that I'm looking at here has,
- 57:04here's the world and then here's the different continents.
- 57:06So like Asia, Africa, the Americas.
- 57:08Then if you want to, within each one of those, they've got a little box that says, well, here's
- 57:14how large of an impact, like for example, Nigeria and Egypt are, you know, have more people
- 57:20in it, I guess, than the other countries.
- 57:22And then they're colored by what their actual values are.
- 57:25But if you want to just focus on say Africa, you can just click on that section of this
- 57:28thing and it just zooms in to show you just that information.
- 57:32And this, you're not going to get this with Matt Plotlet.
- 57:35No.
- 57:35Right?
- 57:36No.
- 57:36No, you're not.
- 57:37And it's...
- 57:38The ability to just dive in and out of the data.
- 57:39Yeah, absolutely.
- 57:40And it is very simple.
- 57:43I use Plotly quite a bit, especially when I want to explore the data.
- 57:47So you want to have a scatterplot or this tree map or any of the other visualizations and you
- 57:53can easily filter or zoom in, zoom out.
- 57:57And yeah.
- 57:58So you're showing some of the other cool kind of unique visualizations that are out there
- 58:02to Plotly that maybe aren't as available in some of the other libraries.
- 58:06Yeah.
- 58:06Yeah.
- 58:07The Sunburst has a real similar...
- 58:09The Sunburst is like a pie chart, but as you interact with it, it like zooms into those
- 58:13sections in pretty amazing ways.
- 58:15Yeah.
- 58:16Well, that is pretty cool.
- 58:17I haven't actually seen that one.
- 58:18That's neat.
- 58:18It's just like...
- 58:20Yeah.
- 58:20It just draws...
- 58:21It just says, I want to explore this data, right?
- 58:24Not only can I, but like, I'm going to.
- 58:26Yes, exactly.
- 58:27Yeah.
- 58:27Cool.
- 58:28Does Plotly have this idea of like a backend server like Altair?
- 58:31Plotly, out of the box, no.
- 58:34It's more, I guess, you know, I have to think behind the scenes, the actual architecture.
- 58:39I don't know, but I do know that you don't have to necessarily worry about your data as
- 58:45much and suddenly having a huge file that shows up in your notebook that you have to deal with
- 58:50Altair.
- 58:50Yeah.
- 58:51Amazing.
- 58:51This is good looking stuff.
- 58:52Yes.
- 58:53And really top marks on the animation and interactivity, sort of diving into the data,
- 58:58right?
- 58:58Yes.
- 58:59Yep.
- 58:59Cool.
- 59:00All right.
- 59:00So that's the building block libraries for the different options.
- 59:04I know there's many other plotting libraries and, you know, we saw in our original graph
- 59:09that there's a bunch here that we didn't touch on, but, you know, these are the ones that
- 59:12you felt are most important right now.
- 59:14Yeah.
- 59:14Yeah.
- 59:15You know, it's interesting.
- 59:16I struggled a little bit with where to draw the line.
- 59:19You know, what else, some of the other things I might want to bring in after I posted about
- 59:23the course, I did get some feedback, you know, how come we didn't talk about a bouquet and
- 59:28panel and hollow viz.
- 59:30And, you know, I think the short and sweet answer was I had to draw the line somewhere.
- 59:36I didn't have as much experience with those libraries, so I didn't dive into them.
- 59:40They are also libraries that are kind of undergoing some, they've undergone changes in the past and
- 59:47they, they are working to clean up their documentation, get the examples cleaned up.
- 59:52So I think it's certainly worth considering those as well.
- 59:56The other one that I would point out that is really interesting to me, but I haven't used
- 1:00:01it, but I think a lot of your listeners might be interested in is a library called Plot9 that
- 1:00:07is meant for people that have used ggplot from an R perspective.
- 1:00:11And it's essentially like a direct port of that to Python.
- 1:00:15So if you really like ggplot and miss it from R, then you can use Plot9 to replicate
- 1:00:22that in Python and it uses a matplotlib behind the scenes.
- 1:00:27And it's, I mean, it looks fascinating to me, especially for people that come from that
- 1:00:32R background.
- 1:00:33Sure.
- 1:00:33That's something we haven't even touched on is like the influence of R and like the parallels
- 1:00:38there.
- 1:00:39And, and...
- 1:00:40Yes.
- 1:00:40Yes.
- 1:00:40We probably want too much.
- 1:00:42So we've got two more things to cover and these are...
- 1:00:45How do I maybe run my code?
- 1:00:46If I want to put it online and make it interactive and let other people interact with it.
- 1:00:50Right?
- 1:00:50So two of them, Streamlit and Plotly Dash.
- 1:00:54Tell us about these.
- 1:00:55Yes.
- 1:00:55So, you know, everything we've talked about now there, especially with Plotly and Altair,
- 1:01:01there is some degree of interactivity.
- 1:01:03But when you want to build a dashboard or want to build more of like an application where
- 1:01:11you can select and filter data, maybe have different visualizations, maybe have complex visualizations
- 1:01:17like, like maps.
- 1:01:19You need something more than just the out of the box Altair or Plotly.
- 1:01:24And Streamlit is a very simple way to wrap a little bit of extra Python code around your
- 1:01:31visualization and you get this interactive application for free.
- 1:01:36And so like the demo you're showing right now is a great, you know, really powerful example
- 1:01:40that shows Uber ride sharing in New York City.
- 1:01:44It has sliders for you to choose what time the pickup happens.
- 1:01:48And then it has these real time visualizations for different parts of the city about how
- 1:01:53many pickups are happening.
- 1:01:55And what's so cool about Streamlit is there is very little additional Python code you need
- 1:02:02to do that.
- 1:02:03So the workflow that I will typically do is I'll do my visualizations in Seaborn or Plotly
- 1:02:10or Altair.
- 1:02:10And then once I realize I need that next level, I can then just easily plop them into a separate
- 1:02:16file with a couple lines of Streamlit code.
- 1:02:18And boom, I've got an interactive application that I can run.
- 1:02:21Yeah.
- 1:02:21It's a really interesting way of programming.
- 1:02:25You basically write a top to bottom procedural script that says, if I, in this case, what are
- 1:02:30we, we're putting, we're putting the hour of a pickup, the hour of data you want to slice
- 1:02:35and visualize.
- 1:02:36And you said, well, if I could write a function that would make a graph given the hour, then
- 1:02:41you just say, and make the web app.
- 1:02:42You know what I mean?
- 1:02:43And it gives you the interactive sliders for all the variables that go in.
- 1:02:47And then you just, as the slider changes, it just recalls your functions.
- 1:02:51And you don't have to know anything about web programming or AJAX or front end code.
- 1:02:56And all of that just happens.
- 1:02:57It's, it's pretty fantastic.
- 1:02:59Streamlit was recently acquired, I think so recently, but within a year or two.
- 1:03:04Yeah, they're acquired by Snowflake for a really large amount of money.
- 1:03:07I know a lot of people are kind of scratching their head at that valuation, not to knock on
- 1:03:12Streamlit.
- 1:03:13I mean, congrats to them, but it's really interesting tool.
- 1:03:15And it will be interesting to see what Snowflake does with them.
- 1:03:19But you know, this tool right now is open source.
- 1:03:22And I do think it is a very powerful, easy way to get a real web native interactive app with
- 1:03:29very little code.
- 1:03:30Yeah, absolutely.
- 1:03:31Let's see.
- 1:03:32Demetrius out there has a question, says, there are all these ways to make graphs quickly,
- 1:03:36but I can't find anywhere on how, any information on how to make an interactive calendar with
- 1:03:42events quickly.
- 1:03:43Yeah, that's a good question.
- 1:03:45I don't know.
- 1:03:46Yeah, I don't know either.
- 1:03:47Of any of these plots that have like a calendar function.
- 1:03:50You don't possibly Streamlit.
- 1:03:52It might.
- 1:03:53Maybe.
- 1:03:53It's interesting that you bring up Streamlit because Streamlit does have like third
- 1:03:57party apps or plugins that you can incorporate.
- 1:04:01that the individual opposed to that.
- 1:04:02I definitely encourage them to take a look at Streamlit and see if there's something out
- 1:04:06there.
- 1:04:06So I interviewed, I believe it was Adrian.
- 1:04:10Let me double check.
- 1:04:11Yeah.
- 1:04:11Adrian Truel back early days, early days of Streamlit.
- 1:04:16I mean, we're talking two years ago before it was acquired about that.
- 1:04:19So people can check that out.
- 1:04:21And I'm somewhat familiar with that.
- 1:04:22The other one, though, that's very fascinating that I don't know about is Dash.
- 1:04:27How's that compared to Streamlit?
- 1:04:28What's the, this comes from the Plotly company as well.
- 1:04:31Yes.
- 1:04:32So Plotly, like we mentioned, is a company.
- 1:04:35They have Dash, which is a much more sophisticated and in-depth platform for developing interactive
- 1:04:45applications or dashboards.
- 1:04:47So whereas Streamlit is a little bit of code, Dash is much more of you kind of are embracing
- 1:04:55HTML and CSS and you're doing callbacks and you have just a ton of flexibility in how you
- 1:05:04structure your application.
- 1:05:05And like this, this demo you're having here, you've got wind speed histograms and you've
- 1:05:10got a line chart that's fully interactive and interactivity between the charts.
- 1:05:16As you choose one, it influences another.
- 1:05:19You can, Dash gives you flexibility to kind of manage the backend as well.
- 1:05:25So you can run it.
- 1:05:27I think it's, it's a Flask server, like on your system, you could do that.
- 1:05:31But if you wanted to do an enterprise grade deployment, you could do that as well.
- 1:05:36It's really, if you can think of it, Dash will probably let you do it.
- 1:05:40And if you're a big enough company and it's mission critical, Dash does have that enterprise
- 1:05:45support where you can pay a company to host it and support it for you.
- 1:05:50You can build, if you, people should go look at the gallery for Plotly Dash.
- 1:05:54There's a bunch of interesting things.
- 1:05:55And one of the areas that stands out, I mean, you've got many, many different types of visualizations
- 1:06:01and whatnot.
- 1:06:01But one of the things that stands out for me is the streaming, streaming data aspect,
- 1:06:05you know, create a dashboard where you hook it up to stock market data or IOT data and
- 1:06:12it just goes, right?
- 1:06:13Right.
- 1:06:14Exactly.
- 1:06:15It's designed to, you know, support a lot of data and low latency and all those kinds
- 1:06:22of things.
- 1:06:22So it's, it's really powerful, but this is one of those areas we talked about everything up
- 1:06:28until now.
- 1:06:29You don't really have to know a whole lot of Python.
- 1:06:31Once you start getting into building Dash, it gets a little more complicated.
- 1:06:36And I think that's where you want to make sure you've got a good solid Python foundation
- 1:06:41before you go and build a dashboard to run your company.
- 1:06:45Yeah.
- 1:06:45But it's very powerful.
- 1:06:47Very powerful.
- 1:06:47Yeah.
- 1:06:48The interactivity between the different elements is also quite interesting.
- 1:06:51It is.
- 1:06:52Yes.
- 1:06:52And that's one of the, I mean, you can do that to some degree with Streamlit, but Dash just
- 1:06:58makes, yeah, you can have a ton of interactivity between the different widgets and the different
- 1:07:04visualizations.
- 1:07:05Cool.
- 1:07:05Well, it looks great.
- 1:07:06If I had a dashboard that looked like this, I'd be proud of it.
- 1:07:09It's not one of those things that's just like, oh, I guess I guess it works.
- 1:07:13You know, no, it looks great.
- 1:07:14Yeah.
- 1:07:14I would be too.
- 1:07:15Yeah.
- 1:07:16Cool.
- 1:07:16All right, Chris, we spent a lot of time on, on this.
- 1:07:19I think we should probably button it up, but.
- 1:07:21Yeah.
- 1:07:22Bring it home.
- 1:07:22A lot of great stuff.
- 1:07:23Yeah.
- 1:07:24A lot of great stuff in the visualization library space.
- 1:07:27I think that's one of the really very powerful aspects of Python is it's just all of these
- 1:07:32tools.
- 1:07:33It's not the language.
- 1:07:34It's not the standard library, which all those, they are important.
- 1:07:37Like it's, people need to think this is sort of what people are talking about when they
- 1:07:41say Python's awesome.
- 1:07:42It's great to use.
- 1:07:43It's not how it does a for loop.
- 1:07:45It's that I can say, you know, dot Andrew's curve.
- 1:07:47Yes.
- 1:07:48Yeah.
- 1:07:49And it all builds, right?
- 1:07:51So if you're, you know, just starting on Python, then you start to build a little pandas
- 1:07:55knowledge.
- 1:07:56You don't have to throw that away and then focus on visualization.
- 1:07:58It all builds on top of it and you can leverage all that knowledge and then all the other wonderful
- 1:08:04libraries that are out there.
- 1:08:05Absolutely.
- 1:08:05All right.
- 1:08:06Before you get out of here though, final two questions.
- 1:08:08If you're going to write some Python code, what editor or editors do you use?
- 1:08:12I'm pretty much a hundred percent VS Code now.
- 1:08:15Right on.
- 1:08:15Even over notebooks.
- 1:08:16Yes.
- 1:08:17Yes.
- 1:08:17I've gotten to where I use the native VS Code notebooks and I really like that.
- 1:08:25Like the comment divider type of style.
- 1:08:27It's just, they, they have continued to update it so much that it just seems like it's a superior
- 1:08:34approach for what I do and how I manage my environments right now.
- 1:08:38Nice.
- 1:08:38And then notable PyPI package or as.
- 1:08:41Yeah.
- 1:08:42I put two in here.
- 1:08:43I don't have a ton of experience with them, but I wanted to call them out because I do
- 1:08:47want to spend some time with it.
- 1:08:49So the first one is Splink.
- 1:08:50I wrote an article a couple of years back about doing data linkage or data duplication.
- 1:08:56And so for, for those of you that aren't familiar, it could be a situation where let's say you
- 1:09:01have your customer database and it's got Chris Moffitt lives at one, two, three main street
- 1:09:05and you have a third party data set.
- 1:09:08And it says, Mr. Moffitt lives at one, two, five main street and street is spelled a street.
- 1:09:14How do you merge all that data together?
- 1:09:17How do you do fuzzy matching?
- 1:09:19And I played around with different options.
- 1:09:22And this Splink is one that's actually came out of the UK from an individual that works
- 1:09:29at the Ministry of Justice, which I think is just a cool name.
- 1:09:32And he talks about using this to, to merge, you know, millions of records together.
- 1:09:38And I think it's a kind of really interesting tool that is something that you can't really
- 1:09:44do in Excel and it's a challenging problem.
- 1:09:46And anytime someone's spent some time on that kind of problem, I think it's really interesting
- 1:09:51and I want to spend some more time looking at that.
- 1:09:52Yeah.
- 1:09:53And they have on that GitHub repo, they got a couple of videos introducing it, which is great.
- 1:09:56Yes.
- 1:09:57And the other one.
- 1:09:58Red frames.
- 1:09:59So this is another one that I've seen come across my Twitter feed a couple of times.
- 1:10:04I have not used it directly, but it is another library for manipulating data that's interoperable
- 1:10:12with pandas, but gives a little bit more of that fluent API where you can kind of string
- 1:10:18all these commands together to modify your data in ways that pandas supports a lot of this,
- 1:10:24but there are certainly some things in pandas that are a little bit clunky.
- 1:10:27And this looks like it's an attempt to try and bring some of that R goodness to Python.
- 1:10:33Yeah.
- 1:10:34It also looks a little bit like bringing SQL to it.
- 1:10:36Yes.
- 1:10:37Yeah.
- 1:10:37Yeah.
- 1:10:37Yeah.
- 1:10:37Certainly there's.
- 1:10:38A filter, a group, a sort, you know, change the names a little bit.
- 1:10:42Yeah.
- 1:10:42Filter away or sort order by, you know, it looks a little bit like SQL.
- 1:10:45It just looks really interesting.
- 1:10:47And like I said, I wanted to get some visibility to it and I haven't used it extensively, but
- 1:10:52certainly want to play around with it a little bit more.
- 1:10:54I thought your listeners might be interested.
- 1:10:56Yeah.
- 1:10:56It looks very cool.
- 1:10:57Thanks for sharing that.
- 1:10:58All right.
- 1:10:59People are interested in Python data visualization.
- 1:11:02They want to know more.
- 1:11:03What do you tell them?
- 1:11:04Hey, check out the course.
- 1:11:05So really excited about the course.
- 1:11:08If you've liked what you've listened to here, the course on Talk Python training has the examples,
- 1:11:15the notebooks for you to go through and learn and play with this on your own.
- 1:11:18And by the end of it, you should be at the point where you can start to apply it to your
- 1:11:22own data.
- 1:11:22So encourage you to check it out.
- 1:11:24And if you do check it out, let me know.
- 1:11:25Be interested to see what you think.
- 1:11:26Yeah.
- 1:11:27It definitely covers all of this in hands-on detail, not just conceptually.
- 1:11:31So yeah.
- 1:11:32Thanks for being here, Chris.
- 1:11:33Thanks for sharing your experience.
- 1:11:35And yeah.
- 1:11:36Happy to have you back on the show.
- 1:11:37Thank you.
- 1:11:38Great discussion.
- 1:11:38Yeah, you bet.
- 1:11:39Bye-bye.
- 1:11:39Bye.
- 1:11:40This has been another episode of Talk Python To Me.
- 1:11:44Thank you to our sponsors.
- 1:11:45Be sure to check out what they're offering.
- 1:11:47It really helps support the show.
- 1:11:48Starting a business is hard.
- 1:11:50Microsoft for Startups, Founders Hub provides all founders at any stage with free resources
- 1:11:57and connections to solve startup challenges.
- 1:11:59Apply for free today at talkpython.fm/founders hub.
- 1:12:04Want to level up your Python?
- 1:12:06We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:12:10Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:12:15And best of all, there's not a subscription in sight.
- 1:12:18Check it out for yourself at training.talkpython.fm.
- 1:12:21Be sure to subscribe to the show.
- 1:12:22Open your favorite podcast app and search for Python.
- 1:12:25We should be right at the top.
- 1:12:27You can also find the iTunes feed at /itunes, the Google Play feed at /play,
- 1:12:32and the direct RSS feed at /rss on talkpython.fm.
- 1:12:36We're live streaming most of our recordings these days.
- 1:12:39If you want to be part of the show and have your comments featured on the air,
- 1:12:43be sure to subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:12:48This is your host, Michael Kennedy.
- 1:12:49Thanks so much for listening.
- 1:12:50I really appreciate it.
- 1:12:51Now get out there and write some Python code.
- 1:12:53I'll see you next time.
- 1:13:14Thank you.