I am looking to change the color of each individual bar in this graph using matplotlib. I just can not seem to figure it out. I have tried using an if/elif/else statement to check the values, but this was unsuccessful. I then tried a for loop and nested the if statements inside of that, but this was unsuccessful as well. The code I have is attached below. (My apologies if it is a mess, or perhaps right in front of my face. I am a student in university, so I am still learning!)
Code:
import string
import pandas as pd
import matplotlib.pyplot as plt
def plot_data(filename, horizontal, vertical):
graph_colors = {'limegreen':'#CCFF33',
'lightgreen':'#9ef01a',
'green':'#70e000',
'darkgreen':'#38b000',
'darkergreen':'#008000',
'darkestgreen':'#007200'}
letters = list(string.ascii_uppercase)
numbers = list(range(0,26))
fields = dict(zip(letters, numbers))
datafile = pd.read_csv(filename)
cols = list(datafile.columns)
title = filename[:-4]
x_axis = cols[fields[horizontal]]
y_axis = cols[fields[vertical]]
datafile = datafile.sort_values(y_axis)
color_of_graph = 'blue'
for row in datafile:
if '31' in row:
color_of_graph = graph_colors['darkestgreen']
datafile.plot(title=title, legend=False, x=x_axis, y=y_axis, kind='bar', color=color_of_graph)
plt.xlabel(x_axis)
plt.ylabel(y_axis)
plt.show()
plot_data("my_file", 'A', 'B')` # A is column a in csv file, B is column b in csv file
Column B (or the plot_data function's third parameter) contains the data that would change the color.
The for loop I tried is:
color_of_graph = 'blue'
for row in datafile:
if '31' in row:
color_of_graph = graph_colors['darkestgreen']
The problem with:
for row in datafile:
...
Is that for x in pandas.df
iterates over column names. So you are basically checking if "31"
is in the name of each column.
If you want to change the color based on 3rd argument and value in that column you should try:
if 31 in datafile[vertical].values:
color_of_graph = graph_colors["darkestgreen"]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.