Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Follow dict color in pandas plotting #59199

Closed
1 of 3 tasks
maddytae opened this issue Jul 7, 2024 · 2 comments
Closed
1 of 3 tasks

ENH: Follow dict color in pandas plotting #59199

maddytae opened this issue Jul 7, 2024 · 2 comments
Assignees
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@maddytae
Copy link

maddytae commented Jul 7, 2024

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

Should it not be more intuitive that plot color strictly follows the color dict and not depend on the order in which col appears in the dataframe?

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create sample data
np.random.seed(0)
df = pd.DataFrame({
    'x': np.random.rand(50),
    'y': np.random.rand(50),
})

# Create bar plot with x in red and y in green using pandas.plot
ax = df[['y','x']].plot(kind='bar', color={'x':'red', 'y':'green','z':'brown'},stacked=True, figsize=(12, 6))

# Customize the plot
plt.title('Bar Plot with X in Red and Y in Green')
plt.xlabel('Index')
plt.ylabel('Values')
plt.legend(['x', 'y'])

# Adjust layout to prevent cutting off labels
plt.tight_layout()

# Show the plot
plt.show()

Feature Description

Also looks like pandas plot is heavily geared towards wide data format and it can be very useful if the approach changes towards the long data format.

Alternative Solutions

Make color follow the color dict.

Additional Context

NA

@maddytae maddytae added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 7, 2024
@ritwizsinha
Copy link
Contributor

@maddytae Thanks for submitting the issue !
It seems like there is some mistake in the legend part of matlplotlib instead of any problem from pandas side. Here is an example

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

# Create sample data
np.random.seed(0)
df = pd.DataFrame({
    'x': [0.1,0.2,0.3],
    'y': [1,2,3],
    'z': [10,20,30]
})
# Create bar plot with x in red and y in green using pandas.plot
ax = df[['z', 'x', 'y']].plot(kind='bar', color={'x':'blue', 'y':'green','z':'red'},stacked=True, figsize=(12, 6))

# Customize the plot
plt.title('Bar Plot with X in Red and Y in Green and z in Blue')
plt.xlabel('Index')
plt.ylabel('Values')
plt.legend(['x', 'y', 'z'])

# Adjust layout to prevent cutting off labels
plt.tight_layout()

# # Show the plot
plt.show()

I have taken non random values for scaling.
This produces the following map.
image

  • x is blue which is the smallest part
  • y is green which is bigger part
  • z is red which is the biggest part.
    The bar is correct, its the legend which labels it wrong, it creates labels as x, y, z whereas labels should be z, x, y because that is what was plotted.

Please correct me if I am wrong
Thanks!

@ritwizsinha
Copy link
Contributor

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

2 participants