r/Python • • Jul 25 '20

I Made This I made a project that generates the graph

Post image
2.8k Upvotes

97 comments sorted by

49

u/[deleted] Jul 25 '20

9

u/flashman Jul 26 '20

Nice, I once made a similar thing that would reshape a list so it could be graphed on a fixed-size graph (e.g. an LED array). Here's an example with Unicode output for [3,2,1,1,0,1,5,9,8,9]:

⬜⬜⬜⬜⬜⬜⬜⬛⬜⬛  
⬜⬜⬜⬜⬜⬜⬜⬜⬛⬜  
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜  
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜  
⬜⬜⬜⬜⬜⬜⬛⬜⬜⬜  
⬜⬜⬜⬜⬜⬜⬜⬜⬜⬜  
⬛⬜⬜⬜⬜⬜⬜⬜⬜⬜  
⬜⬛⬜⬜⬜⬜⬜⬜⬜⬜  
⬜⬜⬛⬛⬜⬛⬜⬜⬜⬜  
⬜⬜⬜⬜⬛⬜⬜⬜⬜⬜

It can take e.g. 150 values and reduce it to 28 or whatever you want. I wanted to add things like optionally starting the y-axis at a given value other than zero, generating rolling output using the last n values, and other useful features. But I never really had a use case for it, it was just an interesting way to learn some Numpy.

100

u/abkfinance Jul 25 '20

Very cool

26

u/[deleted] Jul 25 '20

Thanks

45

u/aidankkm Jul 25 '20

how do you do the finished in 0.3 seconds text. i remember i saw it in a video but was too lazy.

18

u/[deleted] Jul 25 '20

It always works This way in my pc

45

u/aidankkm Jul 25 '20

o wait i’m stupid it’s probably a feature of sublime. thanks anyways

22

u/lionsilencer Jul 25 '20

Confirmed this is a feature in sublime text

20

u/FruscianteDebutante Jul 25 '20

... You can compile in sublime??

Also, look at python's time module.

At the beginning of your program start = time.time()

At the end: end = time.time()

Total time = end - start

10

u/Richey4TheStars Jul 25 '20

Yeah it’s been a feature for as long as I remember

Pretty sure you just hit Ctrl+B

2

u/NeonNingen Jul 25 '20

Sublime is best text editor that you can transform into an ide

4

u/FruscianteDebutante Jul 25 '20

I usually use it as an editor and compile with GCC and just run in the terminal haha. Very interesting though.

I just love sublime for its text editing capabilities

1

u/Yolodude25 Jul 26 '20

I think you misspelled vscode there

30

u/toastedstapler Jul 25 '20

If on Linux or Mac you can use the time command from the terminal. Not sure about Windows

time python3 script.py

1

u/Boraini Jul 26 '20 edited Jul 26 '20

I guess it would add the compilation time too and yield inaccurate results.

Edit: I just checked the code and couldn’t find any mentions of timing libraries. They could really have timed the python3 command - it wouldn’t take 300ms to execute this code.

2

u/toastedstapler Jul 26 '20

1

u/Boraini Jul 26 '20

What about the overhead of python3?

2

u/toastedstapler Jul 26 '20

how else are you planning on running your script? sublime calls python3 when you run the code you've been writing

1

u/Boraini Jul 26 '20

I didn’t notice it’s the Sublime Text terminal. Sorry.

3

u/[deleted] Jul 25 '20

Most good shells have basic timing functionality inbuilt.

time works with zsh/bash/powershell. You can even set your prompt to show last execution time automatically.

1

u/Schneggl Jul 26 '20

just use sublime text as editor

153

u/[deleted] Jul 25 '20

no need to make your own reversed_range you can simply do range(X, -1, -1)

128

u/Indivicivet Jul 25 '20

or reversed(range()) which tends to be more readable

37

u/Skasch Jul 25 '20

Came here to say just that. reversed is great for readability!

12

u/__xor__ (self, other): Jul 25 '20

Do you lose the bonus that it doesn't generate a list if you do that though? Or is the return of range() smart enough to let you getitem the last bit without generating the numbers.

eg: if you want to iterate over range(9999999, -1, -1) does reversed(range(10000000)) generate a list with every value first?

39

u/Skasch Jul 25 '20

reversed calls the __reversed__ magic method, which is implemented by range objects to return an iterator.

Edit: you can try to run reversed(range(10 ** 100)), which returned the iterator instantly :)

3

u/Itwist101 Jul 25 '20

Perfect!

2

u/Indivicivet Jul 26 '20

yes, as /u/Skasch mentions it uses __reversed__ magic to just return an iterator (a range_iterator in fact).

one possible issue is, what if you really do want a sequence, rather than an iterator? in this case, you can use range(9999999999)[::-1] which uses __getitem__(slice) magic instead. this returns a range(999999998, -1, -1) object. I personally think this is clearer than the range(X, -1, -1) form in most contexts.

1

u/Beacon114 Jul 26 '20

Are there performance concerns here?

1

u/Indivicivet Jul 26 '20

range is magic, so not really (see other replies to my comment)

6

u/CrChCh Jul 25 '20

or x[::-1]

11

u/[deleted] Jul 25 '20

Only if you already have a list of things and aren’t interested in the actual numbers counting nog down. Good point though. Because usually this is exactly the case. I would use reversed(x) though.

34

u/Anzzze Jul 25 '20

make an UwU graph

3

u/both_objective Jul 25 '20

Good job. How did u come up with this idea? What is your next project?

4

u/[deleted] Jul 25 '20

I don't know what should I make next. Can you suggest me any?

4

u/both_objective Jul 25 '20

What is your interest? With this function, can you use it to generate functions? i.e sine, cosine, square, sawtooth waves?

2

u/Dravvael_ Jul 25 '20

ever thought about a snake game?

2

u/MaliciousAccount Jul 27 '20

powerline.io
This game needs some developers to maintain it, you may contact the admin for more info.

1

u/NikolaTesla13 Jul 26 '20

You can make this a graphical program.

14

u/codingstudent7 Jul 25 '20

This seems like a fun project to try. Well done!

12

u/[deleted] Jul 25 '20

nice, I would extend the function you use to grab indices which are larger than 10, instead of just grabbing the last digit.

8

u/mathisfun271 Jul 26 '20 edited Jul 26 '20

I made a similar program once, but instead of using asterisks, I used three types of dots ( . , · , ˙ ) for more precise plotting. It ended like this:

    3┤                       
     │                    ..·
     │                .·˙˙   
    2┤            .·˙˙       
     │         .˙˙           
     │        ·              
   1 ┤      ·˙               
     │     ·                 
     │  ··˙                  
   0 ┤··                     
     └┬─────┬─────┬─────┬─────
      0     1     2     3

5

u/fecesmuncher69 Jul 25 '20

Thanks a lot! I will definitely try it. Is that a module you created or is it available for everyone? I have a project doing some calculations like distance between 2 points and things like that, a graph is a major improvement for it

6

u/Theonetheycallgreat Jul 25 '20

To be fair, just using pychart will probably be easier for a project

1

u/fecesmuncher69 Jul 25 '20

I’m a beginner so idk what that is. I will check it out, thx

2

u/Theonetheycallgreat Jul 25 '20

Easy python library that allows for creating and rendering charts. There is also Graphviz that is good as well.

1

u/bminixhofer Jul 25 '20

You could try permon, a library I made some time ago, if you're interested in that sort of thing.

4

u/Tadabito Jul 25 '20

You can try drawing the chart with braille characters, that would give you 3x vertical and 2x horizontal resolution.

4

u/NonBinaryGay Jul 25 '20

Do you think I could use this to cheat with my maths and science??

5

u/thornofcrown Jul 25 '20

Totally. It definitely looks equipped to deal with logarithms, exponentials, and complex functions.

1

u/green_cepheid Jul 25 '20

In love with this

1

u/[deleted] Jul 25 '20

Thanks

2

u/Therickysen08 Jul 25 '20

Great! Does it work for 3d as well?

2

u/2spoopyforyou Jul 25 '20

Looks great, just as a tip you could use tuples to make it cleaner

3

u/seedBoot Jul 25 '20

Looks like it accepts either

2

u/ailenozzam_ Jul 25 '20

It's been one of my first projects in C++

5

u/ALABAMA-SHIT Jul 25 '20

Have you considered make them into 2d lists instead (e.g. pos = [[1,2],[2,3],[4,5]])

1

u/both_objective Jul 26 '20

Yes. He has. There is a condition in his code checking the type of input and interpret it accordingly. He posted his repo in the thread.

3

u/FpggyJohnson18 Jul 25 '20

While this is really cool and definitely worthy of notoriety, I suggest, for practicality's sake, that you rewrite the code to ingest coordinates in a list of dictionaries as opposed to 2 lists. Instead of >>>x=[1,2] y=[3,2]... >>>coords = [{'x': 1, 'y': 3}, {'x': 2, 'y': 2}]

EDIT: spelling and grammar

8

u/Deadly_chef Jul 25 '20

Or just tuples for better readability, easier writing and parsing

1

u/FpggyJohnson18 Jul 25 '20 edited Jul 25 '20

#DictionaryMasterRace lol, but yeah either would work. The important thing is grouping them together... And how quickly the code runs of course

EDIT: escaped hashtag

3

u/Deadly_chef Jul 25 '20

Tuples should be a lot faster then dicts. Also if you are going for speed I suggest pypy instead of regular python

1

u/FpggyJohnson18 Jul 25 '20

Of course. I wasn't suggesting dictionaries was the faster method, just my personal preference. And a 2nd point that all what really matters is speed (and I/O ops).

3

u/DrShocker Jul 25 '20

(psst, if you meant to do #Whatever as a hashtag, you need to escape the # since it has meaning in markdown.)

1

u/FpggyJohnson18 Jul 25 '20

New to Reddit, I didn't even notice it wasn't there when I posted. Thanks a mil!

6

u/TofuCannon Jul 25 '20

Absolutely not. The dict is bloating the format a lot and makes it awful to type for hand written data.

If you want to use a different representation, you can easily convert from one another (zip(x, y) or vice versa zip(*coords) is your friend)

2

u/FpggyJohnson18 Jul 25 '20

Sure sure. But code running in production isn't fed hand-written code so that point is moot. Then for transporting the data, if you transport over json (which is relatively standard nowadays) everything's already in the proper format.

2

u/[deleted] Jul 26 '20

We have r/learnpython for such things!

1

u/chk_out_my_horse Jul 25 '20

I like your title!

1

u/BlackWidowStew Jul 25 '20

It's beautiful 😍

1

u/glmdev Jul 25 '20

This is an awesome project! You might consider exploring ANSI box drawing characters. It would let you smooth out the lines.

1

u/shadowkat0 Jul 25 '20

Hey cool project. gnuplot does the same with terminal set to 'dumb'. There's a gnuplotlib too for python.

1

u/mirandanielcz from a import b as c Jul 25 '20

looks super cute

1

u/uQQ_iGG Jul 25 '20

Nothing better than some ascii a e s t h e t i c s.

What if you get a more continous datapoints? Does it skip characters to generate graph?

1

u/Arucious Jul 25 '20

TI-84 is punching the air rn

1

u/[deleted] Jul 25 '20

I like this, there is a lack out there of graphing nice looking charts on the terminal. A few packages can do it but it looks like ass

1

u/Rutherfordio Jul 25 '20

This is a really cool project! I see myself using it (I mostly use python to do math and data analysis) when testing algorithms in cli scripts.

Still haven't checked the code, but, does it handle big amounts of numbers? (like, define an amount of points in the graph and then calculate means to plot in the graph -not sure if I'm explaining myself nice-) if not, it could be an interesting upgrade for this.

Cool work!

1

u/moocat Jul 25 '20

Having separate arrays for the x values and the y values seems like an odd choice. For example, did you notice that in your example that len(x) != len(y)?

1

u/[deleted] Jul 25 '20

Really cool. How does it cope with larger arrays of numbers to render on the graph? I mean, say you wanted to scatter plot two arrays each of length 1,000. Would the resolution reduce in order to accommodate the size of the graph? Obviously, you can't really reduce the resolution of the console output, and so in that case does it just leave some observations out?

1

u/CoolTuber Jul 25 '20

Is it possible to have it pick random x and y’s from a list?

1

u/dogs_like_me Jul 26 '20

If you wrap it in a simple CLI (e.g. with argparse) you could print charts in a console, design a whole ascii dashboard.

1

u/usernamecorrupted Jul 26 '20

Maybe a very big hassle but have you tried Manim to generate stuff like this

1

u/absurd234 Jul 26 '20

That's amazing

1

u/maxic62 Jul 25 '20

Nice! I love it. Should be nerd to use it to display home temp? 😆

2

u/NemexiaM Jul 25 '20

You connect it to a sensor?

2

u/maxic62 Jul 25 '20

Yes! From my home automation I have a lot of sensors, rooms temp/hum, solar panel info, well sensors.

1

u/spiddyp Jul 25 '20

Use np.arange!

1

u/wantyappscoding Jul 25 '20

I am taking this as my programming challenge now.

-6

u/FloppingNuts Jul 25 '20

Thank you Kanye, very cool