Making Bar Chart Race with Python | Data Science

Поделиться
HTML-код
  • Опубликовано: 11 ноя 2024

Комментарии • 87

  • @andresberejnoi
    @andresberejnoi  2 года назад +2

    In case anyone wants to follow the code, I created a subfolder in one of my repositories. The folder contains the notebook and the small puppy weights dataset to run it correctly. Here is the link:
    github.com/andresberejnoi/PublicNotebooks/tree/master/Puppy%20Bar%20Chart%20Race

  • @Jnile49
    @Jnile49 4 года назад +4

    Very informative and well done! Keep up the good work! Looking forward to more videos.

  • @brendansob
    @brendansob Год назад +1

    Came for the Data Viz tutorial, stayed for the puppies.

    • @andresberejnoi
      @andresberejnoi  Год назад

      Thank you. They are very cute there. I'll be updating this video to use Manim instead in the near future.

  • @subhamraj2528
    @subhamraj2528 Год назад +1

    Hey man ....after installing your package in CMD ...my Jupiter note book takes only the last version of bar_chart_race and unable to update the image label....how can I use your edited dictionary?

    • @andresberejnoi
      @andresberejnoi  Год назад

      Hi, sorry for the delay. What command are you using to install the package?
      Here is a reply I wrote to someone else a while ago on how to install my modified branch. Maybe it will be what you need. Let me know if this helps:
      "Thanks, and of course, I'll see if I can clarify the installation part. Keep in mind that the instructions I'll describe assume you are on a Linux system, but they most likely will work on Windows as well. However, read the whole message before starting because I show an alternative approach at the end.
      One way to install it is by first cloning the repository into your local computer.
      You can do that from the command line with this:
      git clone git@github.com:andresberejnoi/bar_chart_race.git
      That will download the repository to whatever folder you executed the command from. Once there, you need to change to the image_labels branch which contains the modifications I made. You can do that with:
      git checkout image_labels
      Now, I recommend that you install the package inside a Conda environment, so make sure you are inside your environment before the next step. Go into the root of your local copy of the repository.
      To install, in the terminal:
      python setup.py install
      and the package should get installed. If you get any errors regarding required packages, try installing them with pip first and then running the command above again.
      ===================
      There is another method to install a python package from a Github repository without having to clone it, although I have not personally used that option, but from what I'm reading, you just need to provide pip with the address of the repository you want to install. Just keep in mind that many people claim this installation method is slow, but I don't think it would be a problem in this case because it is not a big project, but I could be wrong.
      In this case, from your terminal, and inside a Conda environment run the following command:
      python -m pip install git+github.com/andresberejnoi/bar_chart_race.git@image_labels
      Notice that at the end of that address we are specifying the name of the branch we are instered in (@image_labels).
      Let me know if you are still having issues with that part or if you encountered any errors with my instructions. I'm not able to check them out myself right now."

  • @aibooster-c5q
    @aibooster-c5q 9 месяцев назад +2

    The video cannot render correctly. At the first from it's well, but then all rows begin to messed up. all rows shringe to one line

    • @andresberejnoi
      @andresberejnoi  9 месяцев назад

      I wonder what the problem is. Are you using the same dataset as in the video, or a different one?
      I would also suggest checking out this more recent blog post about bar chart races using Manim instead. Manim is the animation engine used for the channel "3Blue1Brown" and I think it's more robust.
      I'll make a video about it at some point, but for mow here is the pos:
      andresberejnoi.com/blog/bar-chart-race-with-manim/

  • @brickit1747
    @brickit1747 Год назад +1

    hi there :). is it possible to change the chart background from gray to white? haven't found the function so far. also, i wonder if it's possible to make the year number that runs along the bottom right an integer and not with a comma (e.g. 2015 and not 2015.0). many thanks in advance :)

    • @andresberejnoi
      @andresberejnoi  Год назад +1

      Hello, thanks for checking out the video.
      You should be able to change the background on the image by using standard matplotlib parameters. There are a few ways of accomplishing this.
      One is to create your own Matplotlib figure and supply it to the function bar_chart_race(fig=, ...) as a parameter.
      In my example notebook, I did not provide this parameter, but you can create your own figure and simply feed it to the function so it uses that one instead. You would set the color on the axes on that figure before passing it to 'bar_chart_race' using Matplotib.
      The function to set the color should be `ax.set_facecolor('white')` on the axes for your figure.
      Look at the function docstring on line 921 to see what the 'fig' parameter is for:
      github.com/andresberejnoi/bar_chart_race/blob/image_labels/bar_chart_race/_bar_chart_race.py
      #============================================
      Another method is to use the parameter 'fig_kwargs' parameter to pass any matplotlib arguments for figure creation (look at line 944 in the above link for some documentation).
      #============================================
      Perhaps one simpler thing you could try first is to set a global axis color at the beginning of the notebook. You could import Matplotlib and change the rcParams dictionary like so:
      ```
      # in my notebook :
      ...
      ...
      import matplotlib.pyplot as plt
      plt.rcParams['axes.facecolor'] = 'white'
      ...
      ...
      ```
      #============================================
      For your question about the dates showing as a float, make sure your date is some kind of DateTime object and not float.
      You can modify the labels using Matplotlib directly. Take a look at Matplotlib's DateFormater class for that. You can take a look at this other notebook of mine that makes use of different formatting styles for Matplotlib plots:
      github.com/andresberejnoi/PublicNotebooks/blob/master/2022%20Crypto%20Portfolio%20Tracking/2022%20Weighted%20Crypto%20Portfolio%20Tracking.ipynb
      Let me know if any of that works for you

    • @brickit1747
      @brickit1747 Год назад +1

      @@andresberejnoi thank you so much for helping that much. i did it finally.

    • @andresberejnoi
      @andresberejnoi  Год назад

      @@brickit1747 That's great. How did you go about it?

  • @spellfrmhell1628
    @spellfrmhell1628 2 года назад +1

    I am following the video and I hope you can help me on this one:
    TypeError: bar_chart_race() got an unexpected keyword argument 'img_label_folder'
    I don't know what to do to fix it

    • @andresberejnoi
      @andresberejnoi  2 года назад +1

      Hi, I would first make sure that you are using the correct branch that implements that feature. It should be the branch named "image_labels". Check the link in the description, or select that branch instead of "master" when you are in the Github page.

    • @spellfrmhell1628
      @spellfrmhell1628 2 года назад

      @@andresberejnoi I managed to get it working but without the images, that is fine for now since it's a learning experience

  • @tomkmb4120
    @tomkmb4120 Год назад +1

    Does anyone know if that pull request to add the images to the bars ever got integrated into the main repo? I'd love to use that feature for something. Great video!

    • @andresberejnoi
      @andresberejnoi  Год назад

      Hi, it never got merged and that is probably due to my original submission not being very polished. However, If you are still interested in creating bar chart animations, I would suggest you take a look at this post I just wrote about bar chart races using Manim (the animation engine created by 3Blue1Brown for his videos):
      andresberejnoi.com/bar-chart-race-with-manim/
      I think Manim is a much nicer tool for these types of animations. Once I have more time, I will make a video about this, but for now, you can read the post and take a look at the Jupyter notebook linked there.

    • @tomkmb4120
      @tomkmb4120 Год назад +1

      @@andresberejnoi Wonderful! Thanks so much for the response and great resource recommendation - I'll check it out.

    • @tomkmb4120
      @tomkmb4120 9 месяцев назад

      This link now has a 404 unfortunately. @@andresberejnoi

  •  3 года назад +2

    I'm a Python beginner and I am completely lost from the beginning when it comes to make racing bar chart. I need help because I don't know where to start.

  • @shrikant7373
    @shrikant7373 Год назад +1

    hey man i need your help
    while using images everything collapse in the video and appear on the right upper cornor of video, this only happens when i use images
    plz reply, thanks in advance

    • @andresberejnoi
      @andresberejnoi  Год назад +1

      Hi, I'm not sure what could cause that issue. Have you tried using the notebook example and replacing things with your data? If I remember correctly, I did not add any correction for image size, so try to see if your images are similar in size to the ones I used in the example.

    • @shrikant7373
      @shrikant7373 Год назад

      ​@@andresberejnoi Thanks, Buddy, for the reply. As you suggested, I had already run your notebook code with your data and images but the results were same. It only happens when i use images, everything goes in the upper right corner and overlaps on each other. It really helps if you have any other suggestions.

    • @chimera-ant
      @chimera-ant Год назад

      @@andresberejnoi I have the same error, it tells me tight layout is not applied as follows. UserWarning: Tight layout not applied. The bottom and top margins cannot be made large enough to accommodate all axes decorations.
      ret_val = anim.save(self.filename, fps=self.fps, writer=self.writer.
      Is there any way to fix this?

    • @ooooooo4321
      @ooooooo4321 Год назад

      It went well on jupyter lab. But same error occurs on google colab. tell me why the reason

    • @shrikant7373
      @shrikant7373 Год назад

      @@chimera-ant i fixed it ,you just need to open main file and comment that "tight layout part", as far as i remember you have to do it two times 😉

  • @recuerdosfilms
    @recuerdosfilms 4 года назад +3

    You are a geniuos!!!

  • @sinahdsh
    @sinahdsh 3 года назад +1

    Hi! I wanted to clone the edited library in colab, but the original version was cloned despite the fact that I had used your git link. what is the solution?

    • @andresberejnoi
      @andresberejnoi  3 года назад

      Hi, I'm not sure what could have happened. Are you sure you used the correct link, with the "image_labels" branch?
      github.com/andresberejnoi/bar_chart_race/tree/image_labels

  • @saniiiboy
    @saniiiboy 3 года назад +2

    Where can I find the script you wrote about the puppy weights? I can't seem to find it in Git. Thanks!

    • @saniiiboy
      @saniiiboy 3 года назад +1

      Would also be nice if you provide the dataset :)

    • @andresberejnoi
      @andresberejnoi  3 года назад +1

      Hi. Thanks for your comment. You can find the code changes in this file:
      github.com/andresberejnoi/bar_chart_race/blob/image_labels/bar_chart_race/_bar_chart_race.py
      in the repository. It is a new branch that I created just for that feature to submit to the original repository.
      The dataset is very basic, with just the weights in grams, and I think only about 250 rows. I'll look for it and upload it to the repository.

    • @saniiiboy
      @saniiiboy 3 года назад

      @@andresberejnoi thanks a lot, really appreciate it! :)

    • @andresberejnoi
      @andresberejnoi  3 года назад +1

      @@saniiiboy Well, I uploaded the dataset to one of my public repositories here:
      github.com/andresberejnoi/PublicNotebooks/blob/master/puppy_weights.csv
      Hope it is useful

    • @saniiiboy
      @saniiiboy 3 года назад +1

      @@andresberejnoi it is, spent way too long making this work properly over the weekend 😁🙏

  • @EugenioBerejnoi
    @EugenioBerejnoi 4 года назад +4

    Excelente, muy bueno.

    • @andresberejnoi
      @andresberejnoi  4 года назад +2

      Gracias. Estuve habiéndolo desde hace unos días

  • @kimsourtat601
    @kimsourtat601 4 года назад +1

    hi admin, I have created the bar chart race plot and it return the image is dark, not fully color. How to deal with this.

    • @andresberejnoi
      @andresberejnoi  4 года назад

      I'll take a look when I get up. Are you referring to the circle images for each bar?

    • @jasonmyang9854
      @jasonmyang9854 3 года назад +1

      @@andresberejnoi
      I also have this problem, the color of the picture is not displayed properly, the example of the national flag I used

    • @andresberejnoi
      @andresberejnoi  3 года назад

      I'm not sure what could be causing the issue.
      My modifications to the library are very experimental and rough, so there are many things that could go wrong. However, the color should not be affected.
      Do you have a snippet of the script where you are getting the issue?

    • @jasonmyang9854
      @jasonmyang9854 3 года назад +1

      @@andresberejnoiThanks a lot for the reply, I think I have found the problem. The error image format is ‘srgb’. We need to convert to 'rgb'.There are two solutions: 1. Re-save the image using screenshots;
      2. Use script conversion.I = Image.open(img_name).convert('RGB')
      I.save(img_name_new)

    • @andresberejnoi
      @andresberejnoi  3 года назад

      That's great. The modifications that I added do not attempt to check for issues. I think a good addition would be to standardize every image read and also have the program resize them automatically.

  • @izzst2971
    @izzst2971 3 года назад +1

    Would there be a way to do it by image links?

    • @andresberejnoi
      @andresberejnoi  3 года назад

      Hi, do you mean links as in the web or some remote server? I'm not entirely sure but that would fall within matplotlib's capabilities, since that is the underlying engine used to render the images

    • @izzst2971
      @izzst2971 3 года назад

      @@andresberejnoi Ah I meant links in the web. also a problem that I ran into was that you couldn't have any images at all if there wasn't an image for every possible label. was wondering if having images on some bars while not having any on others is possible

  • @angeo10
    @angeo10 3 года назад +1

    Great video congratulations, could you share the notebook?

    • @andresberejnoi
      @andresberejnoi  3 года назад +1

      I'll look if I saved that notebook and add it to the description.

  • @xuzhang2431
    @xuzhang2431 2 года назад +1

    Thank you for your great video. I like you added images into the plot. Could you please add your jupter notebook file into your github repo? Many thanks

    • @andresberejnoi
      @andresberejnoi  2 года назад

      I'm glad to hear. I'll take a look at the notebook and clean it up. I'll try to upload it today.

    • @andresberejnoi
      @andresberejnoi  2 года назад

      Hey, here is the link to the notebook (I also added it as a pinned comment):
      github.com/andresberejnoi/PublicNotebooks/tree/master/Puppy%20Bar%20Chart%20Race

    • @xuzhang2431
      @xuzhang2431 2 года назад +1

      @@andresberejnoi Thank you so much!

  • @iliaszografos4064
    @iliaszografos4064 3 года назад +1

    Hey Andres! Thanks for the video, it's really helpful...but I have a question for you. When I write the script for the bar chart race, I get this error message "bar_chart_race() got an unexpected keyword argument 'img_label_folder'". Could you imagine what's going wrong?

    • @andresberejnoi
      @andresberejnoi  3 года назад +3

      Hi, are you sure you are using the modified branch "image_label" from the repository and not the master branch?

    • @iliaszografos4064
      @iliaszografos4064 3 года назад +1

      @@andresberejnoi To be honest, I'm not 100% sure! Could you please send me the link from the modified branch? I'm really fresh into coding, so sorry if this sounds too silly☺️

    • @andresberejnoi
      @andresberejnoi  3 года назад

      ​@@iliaszografos4064 Sure! Here is the link to my modified branch (it is also in the description of the video): github.com/andresberejnoi/bar_chart_race/tree/image_labels
      In any case, just double check on the Github page that you are using "image_labels" as the branch. You can see the branch name just above the list of files and slightly to the left.

    • @iliaszografos4064
      @iliaszografos4064 3 года назад +1

      @@andresberejnoi Thanks for answering!! I'll try it again with the modified branch.

    • @muhammadabuelenin7233
      @muhammadabuelenin7233 2 года назад

      @@iliaszografos4064 @Andrés Berejnoi i couldnt solve this issue, can you please help me!

  • @disfordata8582
    @disfordata8582 4 года назад +1

    good stuff, thanks

    • @andresberejnoi
      @andresberejnoi  4 года назад

      Hey thanks. I'm glad you liked it. I have been working on a new video about computer vision with opencv and python, but it has been hard to find the time to edit it

  • @somukdutta
    @somukdutta 4 года назад +1

    thank you

    • @andresberejnoi
      @andresberejnoi  4 года назад

      I hope it was useful. I'll be posting more python videos

  • @FreestyleStats
    @FreestyleStats 3 года назад +1

    Hi, I love what you did. How can I install the modifications you made?

    • @FreestyleStats
      @FreestyleStats 3 года назад +1

      (the feature)

    • @FreestyleStats
      @FreestyleStats 3 года назад

      I'm using this code but doesn't work:
      !pip install git+github.com/andresberejnoi/bar_chart_race/tree/image_labels

    • @andresberejnoi
      @andresberejnoi  3 года назад +1

      @@FreestyleStats you can install the package from the modified branch in my github repository. Go to the link and select the branch "image_labels", not master.
      Since this is a package, you can use the setup.py file to install it. I recommend to install it inside an environment.
      From the terminal, you can use "python setup.py install" in the main repository directory. Make sure you are using the altered branch and not master, as that one is the same as the original.
      Also keep in mind that you might need to tinkle with the image parameters in the code to get them to display at a proper size. It is very rough and not general right now and I haven't put much time into polishing those parts to become automatic, but you should try it. If you make some improvements to the code consider contributing them to the repository and maybe one day it can be added to the official package.

    • @chummicrisologo2474
      @chummicrisologo2474 3 года назад +1

      @@andresberejnoi Hi! Sorry im still a bit confused as to how to install your modified package :( Could yo give a short step by step tutorial reply? Sick edit btw!

    • @andresberejnoi
      @andresberejnoi  3 года назад +3

      Thanks, and of course, I'll see if I can clarify the installation part. Keep in mind that the instructions I'll describe assume you are on a Linux system, but they most likely will work on Windows as well. However, read the whole message before starting because I show an alternative approach at the end.
      One way to install it is by first cloning the repository into your local computer.
      You can do that from the command line with this:
      git clone git@github.com:andresberejnoi/bar_chart_race.git
      That will download the repository to whatever folder you executed the command from. Once there, you need to change to the image_labels branch which contains the modifications I made. You can do that with:
      git checkout image_labels
      Now, I recommend that you install the package inside a Conda environment, so make sure you are inside your environment before the next step. Go into the root of your local copy of the repository.
      To install, in the terminal:
      python setup.py install
      and the package should get installed. If you get any errors regarding required packages, try installing them with pip first and then running the command above again.
      ===================
      There is another method to install a python package from a Github repository without having to clone it, although I have not personally used that option, but from what I'm reading, you just need to provide pip with the address of the repository you want to install. Just keep in mind that many people claim this installation method is slow, but I don't think it would be a problem in this case because it is not a big project, but I could be wrong.
      In this case, from your terminal, and inside a Conda environment run the following command:
      python -m pip install git+github.com/andresberejnoi/bar_chart_race.git@image_labels
      Notice that at the end of that address we are specifying the name of the branch we are instered in (@image_labels).
      Let me know if you are still having issues with that part or if you encountered any errors with my instructions. I'm not able to check them out myself right now.

  • @wardbrutal
    @wardbrutal 2 года назад

    i cant set speed of animation

    • @andresberejnoi
      @andresberejnoi  2 года назад +1

      You can pass the parameter "period_length=xxx" to the bar_chart_race function, where xxx is in milliseconds. This number represents how long to animate each row in your data. There is a simple example on the README.md from the repository.

    • @wardbrutal
      @wardbrutal 2 года назад

      @@andresberejnoi thx