简体   繁体   中英

plotting specific columns based on user input

I have a dataframe with the following setup:

      a     c     g     s
Ind   b     d     t     d 
0    11    12     22    33
1    13    14     44    101

The goal is to receive input from user via GUI, save the input as a list and compare it with the list of the headers in the dataframe. If the the two match, then plot the column where they match (the column will be the y-axis and index will be the x-axis).

For example, if user selects [('c','d')] then I would like the code to plot that column. Here is what I have so far.

df = pd.read_csv('foo.csv',sep=r'\s*,\s*', encoding='ascii', engine='python')
header_list = [('a','b'),('c','d'),('g','t'),('s','d')]
user_Input_list = [('c','d')]
sub_list = []

for contents in header_list:
    for contents2 in user_Input_list:
        if contents == contents2:
            ax = df.reset_index().plot(x='Ind', y=x[header_list], kind='bar')
            for p in ax.patches:
                ax.annotate('{:.2E}'.format(Decimal(str(p.get_height()))),
                            (p.get_x(),p.get_height()))
            plt.tight_layout()
            plt.show()

I think the problem lies in how I am trying to select the y-axis with y=x[header_list] .

Edit

Here is the Error message I get when I run the above code.

Traceback (most recent call last):
  File "/home/JM9/PycharmProjects/subfolder/subfolder.py", line 360, in <module>
    ax = x.reset_index().plot(x='Ind', y=x[header_list], kind='bar')
  File "/home/JM9/PycharmProjects/subfolder/venv/lib/python3.6/site-packages/pandas/plotting/_core.py", line 780, in __call__
    data = data[y].copy()
  File "/home/JM9/PycharmProjects/subfolder/venv/lib/python3.6/site-packages/pandas/core/frame.py", line 2982, in __getitem__
    return self._getitem_frame(key)
  File "/home/JM9/PycharmProjects/subfolder/venv/lib/python3.6/site-packages/pandas/core/frame.py", line 3081, in _getitem_frame
    raise ValueError("Must pass DataFrame with boolean values only")
ValueError: Must pass DataFrame with boolean values only

I'm having trouble figuring out your example code, one thing that could help to solve the problem is to re-create a simpler version of what you're trying to do, without hard-to-deal-with column names.

import random
import pandas as pd

data={
    'a': [random.randint(0, 50) for i in range(4)],
    'b': [random.randint(0, 50) for i in range(4)],
    'c': [random.randint(0, 50) for i in range(4)]
}

df = pd.DataFrame(data)
df.index = df.index.rename('Ind')

user_input = 'b'

if user_input in df.columns:
    ax = df[user_input].plot(x='Ind', kind='bar')

Some useful takeaways for you: (a) instead of looping you can perform a simple test to see if a user's input is equal to a data frame column ( if user_input in df: ) and (b) you can call .plot() against individual columns.

Edit: changed 'b' to user_input

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.

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