简体   繁体   English

有没有办法使用 Python 中的 matplotlib/pandas 模块根据 csv 文件提供的数据更改条形图的颜色?

[英]Is there a way to change the color of a bar graph based on the data provided from a csv file, using the matplotlib/pandas module in Python?

I am looking to change the color of each individual bar in this graph using matplotlib.我希望使用 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.我曾尝试使用 if/elif/else 语句来检查值,但这不成功。 I then tried a for loop and nested the if statements inside of that, but this was unsuccessful as well.然后我尝试了一个 for 循环并将 if 语句嵌套在其中,但这也没有成功。 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. B 列(或 plot_data 函数的第三个参数)包含会改变颜色的数据。

The for loop I tried is:我尝试的 for 循环是:

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.for x in pandas.df迭代列名。 So you are basically checking if "31" is in the name of each column.因此,您基本上是在检查"31"是否在每列的名称中。

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"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM