Skip to content

This commit introduces a custom bar plot function #26314

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

Closed

Conversation

Saunakghosh10
Copy link

Commit

This commit introduces a custom bar plot function and provides an example of usage inside the code. The custom bar plot function allows users to create bar plots with customizable labels, values, and colours. It enhances the functionality of the code by providing a convenient way to visualize data.

The custom_bar_plot function takes in an array-like object of labels representing the categories, an array-like object of values representing the values for each category, and an optional array-like object of colours specifying the custom colours for each bar. If the colours parameter is not provided, default colours from the Viridis colourmap are used.

The example usage section demonstrates how to use the custom_bar_plot function by creating a bar plot with sample data. The labels list contains the categories, the values list contains the corresponding values, and the colours list specifies custom colours for each bar. Running the code will generate a bar plot with labelled categories, values, and a title.

This commit improves the code by adding a versatile function for custom bar plotting, enhancing the visualization capabilities and allowing users to tailor the appearance of their plots to their specific needs.

@rcomer rcomer mentioned this pull request Jul 15, 2023
5 tasks
@rcomer
Copy link
Member

rcomer commented Jul 15, 2023

Thank you for your contribution @Saunakghosh10!

Please could you revert the changes to setup.py. They seem unrelated to the intent of this PR and I think they are causing all the failures in our automated checks below.

There are also several style issues highlighted by the flake8 checks. If these could be addressed, it will make it easier to review this in more detail. See the second bullet here for how to run flake8 checks locally.

@Saunakghosh10
Copy link
Author

okay thank you

@story645
Copy link
Member

story645 commented Jul 16, 2023

Thanks for making this example! Can you please write it using ax. API to conform to our style guidelines? https://matplotlib.org/devdocs/devel/document.html#write-examples-and-tutorials

If you're not sure how, please ask here or in our chat.

Eta: also please revert the changes to 3D bar since there isn't anything especially related to 3D here.

@timhoffm
Copy link
Member

timhoffm commented Jul 16, 2023

Thanks for the desire to improve our examples!

Please describe more clearly what the purpose is. As of now, I do not see the benefit of adding this

create bar plots with customizable labels, values, and colours.

is already provided by the builtin bar() function. The only new aspect is the possibility to use colors from a colormap. However,

  1. I'm not sure that this warrants a new example. It's basically an orthogonal concept and we generally try to avoid these combinations because they'd result in vast amouts of examples ("bars with colors from a colormap", "line plots with colors from a colormap", "boxplots with colors from a colormap", ...) which makes finding relevant information much more difficult.

  2. If there are good reasons to show this combination, I'd like to see a more practical example:
    a) The function essentially boils down to

    colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))
    plt.bar(labels, values, colors=colors)
    

    with additional hard-coded x/ylabels, title etc. This is not a function one could use in a real-world example. The above two lines are enough to demonstrate the concept of getting colors from a colormap.

    b) The reason for individual colors should be to encode additional information. Just encoding the sequence of bars (i.e. effectively overlaying the bar plot with a discretized color gradient) is thus not a good example. If we want to show this, we'd need a more real-world example where the coloring actually conveys additional information.

@rcomer
Copy link
Member

rcomer commented Jul 16, 2023

@timhoffm I haven't looked in detail, but I think this one is in response to @story645's suggestion at #26310.

@Saunakghosh10
Copy link
Author

Thanks for making this example! Can you please write it using ax. API to conform to our style guidelines? https://matplotlib.org/devdocs/devel/document.html#write-examples-and-tutorials

If you're not sure how, please ask here or in our chat.

Eta: also please revert the changes to 3D bar since there isn't anything especially related to 3D here.

import matplotlib.pyplot as plt
import numpy as np

def custom_bar_plot(labels, values, colors=None):
"""
Custom bar plot function that plots bars with optional custom colors.

Parameters:
    labels (array-like): Labels for each bar.
    values (array-like): Values for each bar.
    colors (array-like, optional): Colors for each bar. If not provided, default colors are used.

Returns:
    None
"""
x = np.arange(len(labels))
if colors is None:
    colors = plt.cm.viridis(np.linspace(0, 1, len(labels)))

fig, ax = plt.subplots()
ax.bar(x, values, color=colors)
ax.set_xlabel('Categories')
ax.set_ylabel('Values')
ax.set_title('Custom Bar Plot')
ax.set_xticks(x)
ax.set_xticklabels(labels)
ax.grid(True)
plt.show()

@story645
Copy link
Member

I'm not sure that this warrants a new example.

Agree, that's why I suggested it as a new section in the bar color demo.

@Saunakghosh10 great that it's now in ax style. I agree w/ @timhoffm that there isn't much utility in this being a function since the functionality you need is just what Tim shows. Can you pull this code out of a function and add some simple dummy data?

@Saunakghosh10
Copy link
Author

okay on it

@story645
Copy link
Member

Also, to revert the changes to setup.py and to the plot_gallery example, try https://git-scm.com/docs/git-reset#Documentation/git-reset.txt-Resetasinglefileintheindex

@rcomer
Copy link
Member

rcomer commented Sep 18, 2023

@Saunakghosh10 are you still interested in working on this?

@Saunakghosh10
Copy link
Author

@Saunakghosh10 are you still interested in working on this?

yes I am and I and I am working on that only tbh..

@rcomer
Copy link
Member

rcomer commented May 11, 2024

I'm going to mark this as "draft" for now. Please mark it as "ready for review" when you are ready. In the meantime, if you need help, feel free to ask questions here. Or you may prefer to ask them in our incubator gitter room.

@rcomer rcomer marked this pull request as draft May 11, 2024 14:57
@timhoffm
Copy link
Member

timhoffm commented Feb 2, 2025

Closing because of the need for additional functionality is not clear.

@timhoffm timhoffm closed this Feb 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Waiting for author
Development

Successfully merging this pull request may close these issues.

5 participants