How to Add Color to a Bar Chart using RGB in Python
I had come up with some nice New York Real Estate Dataset and the challenge is to come up with a chart answering a specific question. For me when I see the data, the first thing that comes up to my mind is where is the cheapest real estate property in New York. Assuming I can afford it and I have the right visa to buy a property in New York. The objective is to list the Top 20 Cheapest Neighborhoods in New York. I will also discuss how I manage to add some color in my otherwise boring chart.
Cleaning the Data
Firstly, I checked my data and found out I am dealing with strings for my Sales Price column. I then decided to see the unique values and saw I have “- ” in my data. I learned from here that it will be best to check your data for unknown spaces when dealing with strings. You can also use strip function in all the cells just to make sure no unintended spaces.
To properly clean my data, I used lambda function to apply strip() function and change the value to 0. Then, changed the data type to float.
Grouping and Sorting the Values
I grouped the values by the NEIGHBORHOOD and take the mean value of the SALES PRICE per Neighborhood. Then, I sorted in descending value. It is just one line of code but it is so powerful. It does three(3) things at one line.
Creating the Chart
To create the Chart, I first made a y-axis list using the Neighborhoods. I will be using this for plotting the chart.
I then created the x and y axis for the chart where I will only get the Top 20 Neighborhoods.
For this part, I wanted to increase the size of the figure of my chart. Initially it was too small to even like read the labels that is why I made it larger.
For this part, I created a random RGB color list. I have 20 colors to make.
I plot the data using a barh chart as I feel, the barh chart can better represent the differences in the top 20 cheapest Neighborhoods to buy a property in New York.
Now the Chart looks a bit better than before. Hopefully, this article helped you a little bit. I am a beginner myself as of writing but doing this kind of project can really stimulate my mind into deeper thinking. Hoping to write more. See you till the next article!.