
Exploring Mathematics with Matplotlib and Python - acangiano
https://programmingzen.com/exploring-mathematics-with-matplotlib-and-python/
======
svat
This is a nice article. For those who have not yet read it (it's short, read
it!), a one-paragraph summary: the author starts with a list of random
numbers. Visualizing it (plotting the numbers, with the list index on the x
axis) suggests / leads to (for the author) curiosity about how often numbers
repeat. Plotting _that_ leads to the question of what the maximum frequency
would be, as a size of the input list. This can lead to a hypothesis, which
one can explore with larger runs. And then after some musings about this
process, the post suddenly ends (leaving the rest to the reader), and gives
the code that was used for plotting.

This article is essentially an encouragement and a reminder of our ability to
do experimental mathematics
([https://en.wikipedia.org/w/index.php?title=Experimental_math...](https://en.wikipedia.org/w/index.php?title=Experimental_mathematics&oldid=899918382)):
there's even a journal for it, and the Wikipedia article on it is worth
reading
([https://en.wikipedia.org/w/index.php?title=Experimental_Math...](https://en.wikipedia.org/w/index.php?title=Experimental_Mathematics_\(journal\)&oldid=894684677)).
See also (I guess I'm just reproducing the first page of search results here)
this article
([https://www.maa.org/external_archive/devlin/devlin_03_09.htm...](https://www.maa.org/external_archive/devlin/devlin_03_09.html)),
these two in the Notices of the AMS ([https://www.ams.org/notices/200505/fea-
borwein.pdf](https://www.ams.org/notices/200505/fea-borwein.pdf),
[http://www.ams.org/notices/199506/levy.pdf](http://www.ams.org/notices/199506/levy.pdf)),
this website
([https://www.experimentalmath.info](https://www.experimentalmath.info)), this
post by Wolfram ([https://blog.stephenwolfram.com/2017/03/two-hours-of-
experim...](https://blog.stephenwolfram.com/2017/03/two-hours-of-experimental-
mathematics/)), and there's even book by V. I. Arnold (besides a couple by
Borwein and Bailey, and others).

Especially in number theory and probability, simple explorations with a
computer can suggest deep conjectures that are yet to be proved.

~~~
mettamage
Layman here

Thank you so much for pointing this out! Experimental mathematics feels like a
missing puzzle piece in which it makes so much more sense.

Quotes are from the wiki article you linked.

> As expressed by Paul Halmos: "Mathematics is not a deductive science—that's
> a cliché. When you try to prove a theorem, you don't just list the
> hypotheses, and then start to reason. What you do is trial and error,
> experimentation, guesswork. You want to find out what the facts are, and
> what you do is in that respect similar to what a laboratory technician
> does."[3]

I wish there were books on how people would describe their complete process
(not only their proof) on how they figured things out.

> Mathematicians have always practised experimental mathematics. Existing
> records of early mathematics, such as Babylonian mathematics, typically
> consist of lists of numerical examples illustrating algebraic identities.
> However, modern mathematics, beginning in the 17th century, developed a
> tradition of publishing results in a final, formal and abstract
> presentation. The numerical examples that may have led a mathematician to
> originally formulate a general theorem were not published, and were
> generally forgotten.

Why is this the case? It seems like it doesn't benefit us other than saving
some paper.

> The following mathematicians and computer scientists have made significant
> contributions to the field of experimental mathematics:

Fabrice Bellard

Donald Knuth

Stephen Wolfram

(among others)

\--> This is so awesome, it also sheds some light into how these people think.

~~~
analog31
This isn't to defend how it's done, but the tradition has been that the
"laboratory technician" skills are learned on the job. This is true of lab
tech work as well. I've taught a number of summer interns how to solder, but
it's not written up in any research paper. Of course that makes it hard if one
isn't preparing to do it as a job.

~~~
jacobolus
> _I 've taught a number of summer interns how to solder, but it's not written
> up in any research paper._

Not in a research paper, but it is described in some nice decades-old training
videos,
[https://www.youtube.com/playlist?list=PL926EC0F1F93C1837](https://www.youtube.com/playlist?list=PL926EC0F1F93C1837)

------
r34
1\. How many people do there need to be in a room so the there is a greater
than 50% chance of at least two of them sharing the same birthday?

2\. How many numbers we have to draw from 365 so the there is a greater than
50% chance of at least two of them are the same?

3\. How many numbers we have to draw from X so the there is a greater than Y%
chance of at least Z of them are the same?

I think X,Y,Z are enough parameters:

Drawing from X=1000 numbers, what is the chance Y that Z=(5,6,7,8...) is the
same?

Sorry, I'm not a matematician, just some breakfast ideas ;)

Edit: Inspired by
[http://datagenetics.com/blog/february72019/index.html](http://datagenetics.com/blog/february72019/index.html)

------
juggernaut390
I’m embarrassed to see such a post upvoted. Also, matplotlib is outdated. If
you want a good visualization tool, it should leverage as many features as it
can to present the most information possible. This includes not just color but
interactive tools like hover tools. A library like bokeh makes this extremely
easy for example. I’m a bit sad to see such posts whose purpose is to
demonstrate how to leverage visualization tools to improve our understanding
of a phenomenon by people holding on to legacy outdated tools. It sends the
wrong message.

~~~
goerz
How is matplotlib outdated? It's just relatively low-level, so it might be
quicker to use high-level tools like seaborn or altair (which use matplotlib
as their backend), if that fits the situation. For heavily customized
publication-ready plots (that means print), there is no alternative to
matplotlib. The other two low-level Python visualization libs, plotly and
bokeh (see [https://pyviz.org/tools.html](https://pyviz.org/tools.html)) focus
on interactive plots, which is an entirely different use case! Bokeh just does
not generate visualizations for print. Thus, it does NOT make matplotlib
obsolete. On top of that, last time I checked, bokeh was far less flexible
than matplotlib.

~~~
nicolaskruchten
Mighty matplotlib isn't going away any time soon, but just because Plotly
makes interactive plots, it doesn't mean you can't also

a) customize every aspect of the chart, from the fonts to the length of the
axis ticks to the legend placement etc (see the full list of thousands of
available customization attributes here:
[https://plot.ly/python/reference/](https://plot.ly/python/reference/))

b) export to raster or vector formats for publication
([https://plot.ly/python/static-image-export/](https://plot.ly/python/static-
image-export/))

c) use high-level grammar-of-graphics-inspired tools like
[https://plotly.express/](https://plotly.express/) to create complex charts in
a single line of code.

~~~
goerz
I haven't really looked at plotly in a very long time. At that time, it was
cloud-based. Having the data I'm plotting sent to ploty's servers, or more
generally, my plots depending on the availability of some remote server, was a
no-go for me. Has this changed?

~~~
nicolaskruchten
Very much so! Plotly.py version 4 is "offline-only" just like matplotlib and
other libraries: [https://medium.com/plotly/plotly-py-4-0-is-here-offline-
only...](https://medium.com/plotly/plotly-py-4-0-is-here-offline-only-express-
first-displayable-anywhere-fc444e5659ee)

~~~
goerz
Very nice! I might give it another try sometime soon, then!

------
enriquto
As much as I love the Python language, it is shameful how it has become a sort
of "schtrumpf"-like addition to any computer-related stuff. Such introductory
tutorials are great (but this one, specifically, would benefit much by having
the code that generates each figure next to it). However, it is really not
necessary to specify that your particular thing is "with Python" as if it
really meant anything fundamental.

What kind of shitty reasoning leads to this? "Oh, let's introduce this
elementary mathematics to the illiterate masses by writing it as a Python
script. Now everyone will understand it!" This is a lack of respect for the
agency of the readers.

~~~
jorge-fundido
Perhaps it's just a factual element. Or perhaps the intention is
`s/python/pragmatic/`. Yeah, python sucks in many ways but is it really so
horrible? I know! Let's ask the reader to get intimate with a static compiler.
If they're "smart" enough to satisfy (or trick) the compiler, then they've
earned the "reward" of being able to execute their program.

There is value is static typing, but there are many instances where that cost
is not worth the reward.

~~~
jacobolus
I think the grandparent poster was just pointing out that this post has very
little to do with python or matplotlib, with the code to generate the plots
just thrown in as an afterthought at the end. Which makes it weird to have
them in the title. (Whether python is a good tool or not is unrelated to this
observation.)

I would expect a post entitled “exploring mathematics with python” to have a
whole lot more python code (inline with the text and better explained instead
of an uncommented blob at the end) and a whole lot more mathematics.

A more accurately descriptive title for this post might be “counting the
repetitions among randomly chosen positive integers”.... which of course isn’t
going to get as many clicks or as many reflexive upvotes from non-readers as a
post promising “exploring mathematics with python” because it doesn’t sound
(and frankly isn’t) all that interesting to most readers. (It might make a
decent short project for middle school students though.)

Personally I flagged the post for its misleading title.

~~~
inimino
What I got out of the post was that exploratory and experimental mathematics
is fun and worthwhile and if you haven't tried it, you should, and by the way,
this experiment uses Python and matplotlib (which some readers may already
know). I think you missed the point of the article.

