Vityata
Vityata

Reputation: 43593

How to add information to the x-axis label of the plotted graph in Python?

import pandas as pd
import matplotlib.pyplot as plt

my_funds = [10, 20, 50, 70, 90, 110]
my_friends = ['Angela', 'Gabi', 'Joanna', 'Marina', 'Xenia', 'Zulu']
my_actions = ['sleep', 'work', 'play','party', 'enjoy', 'live']
df = pd.DataFrame({'Friends': my_friends, 'Funds':my_funds, 'Actions': my_actions})

produces:

enter image description here

Then, I am plotting it like this:

df.plot (kind='bar', x = "Friends" , y = 'Funds', color='red', figsize=(5,5))

getting the following:

enter image description here

What is the goal?

The same graph, but instead of "Angela" to have "Angela - sleep" written on the x-axis. The "sleep" comes from the "Action" column.

Further [Gabi - work, Joanna - play, Marina - party, Xenia - enjoy, Zulu - live]

Upvotes: 0

Views: 53

Answers (2)

Celius Stingher
Celius Stingher

Reputation: 18377

For the sake of this question it is easier to create an extra column with the values you desire and then pass it to the df.plot():

df['Friends_actions'] = df['Friends'] + " " + df['Actions']
df.plot(kind='bar', x = "Friends_actions" , y = 'Funds', color='red', figsize=(5,5))

Output: enter image description here

Upvotes: 2

tdpr
tdpr

Reputation: 272

A solution could be to create a new column in your DataFrame:

df["Friend-Action"] = [f"{friend} -> {action}" for friend, action in zip(df["Friends"], df["Actions"])]

Then, plot this column:

df.plot (kind='bar', x = "Friend-Action" , y = 'Funds', color='red', figsize=(5,5))

enter image description here

Upvotes: 2

Related Questions