0

Please show mercy with me. I am relatively new with Plotly :-)

Currenlty, I am plotting a histogram which also marks with a line serveral values of interest. In addition, I want to provide a legend which displays labels each of the different values. Therefore, I am using the following function:

def create_histogram(data: pd.DataFrame, property: str, inn_ids: list[str]) -> str:
    """
    Create a histogram for the given property amd marks all values of interest.

    Args:
        df (pd.DataFrame): _description_
        property (str): the property name which defines the histogram for plotting the histo.
        inn_ids (list[str]): the list defines all values which will be marked with a horizontal line in the plot.

    Returns:
        str: It returns the plot in json format.
    """

    for inn_id in inn_ids:
        if inn_id not in list(data["INN"]):
            raise ValueError(f"The INN ID {inn_id} does not exist in the data.")

    unit = ""

    fig = px.histogram(data, x=property)

    x_axis_description = load_property_description(property, "name")
    unit = load_property_description(property, "unit")

    if unit != property:
        x_axis_description = f"{x_axis_description} (in {unit})"

    fig.update_xaxes(title_text=x_axis_description)

    fig.update_yaxes(title_text="Count")

    for index, inn_id in enumerate(inn_ids):
        inn_value = data.loc[data["INN"] == inn_id, property].values[0]

        fig.add_trace(
            go.Scatter(
                x=[inn_value, inn_value],
                y=[0, max(data[property])],
                mode="lines",
                name=f"{inn_id} ({property}={inn_value})",
                line=dict(color=plot_line_colors[index], width=3.5, dash="dash"),
            )
        )

    fig.update_layout(showlegend=True)
    graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    return graph_json

Unfortunately, the line y=[0, max(data[property])], draws a to long line which then changes the y scale of the whole histogram. I am using plotly for python. The graph looks then like that: enter image description here

Are there any tricks how can I fix that by finding of thew histogram without the line the highest y value on the y scale or is there another trick?

With the debugger finding the orginal scale of the histogram :-)

1 Answers1

0

You could find the max count of the graph then use that value to set the range for the y-axis. Just be sure to change the number of bins as needed but it looks like 8 from the image. It would look something like this:

import numpy as np

def create_histogram(data: pd.DataFrame, property: str, inn_ids: list[str]) -> str:
    """
    Create a histogram for the given property amd marks all values of interest.

    Args:
        df (pd.DataFrame): _description_
        property (str): the property name which defines the histogram for plotting the histo.
        inn_ids (list[str]): the list defines all values which will be marked with a horizontal line in the plot.

    Returns:
        str: It returns the plot in json format.
    """

    for inn_id in inn_ids:
        if inn_id not in list(data["INN"]):
            raise ValueError(f"The INN ID {inn_id} does not exist in the data.")

    unit = ""

    counts, edges = np.histogram(data[property], bins=8)  # adjust the number of bins as needed

    fig = px.histogram(data, x=property)

    x_axis_description = load_property_description(property, "name")
    unit = load_property_description(property, "unit")

    if unit != property:
        x_axis_description = f"{x_axis_description} (in {unit})"

    fig.update_xaxes(title_text=x_axis_description)

    fig.update_yaxes(title_text="Count", range=[0, max(counts)*1.2])  # adjust the range multiplier as needed

    for index, inn_id in enumerate(inn_ids):
        inn_value = data.loc[data["INN"] == inn_id, property].values[0]

        fig.add_trace(
            go.Scatter(
                x=[inn_value, inn_value],
                y=[0, max(counts)*1.2],
                mode="lines",
                name=f"{inn_id} ({property}={inn_value})",
                line=dict(color=plot_line_colors[index], width=3.5, dash="dash"),
            )
        )

    fig.update_layout(showlegend=True)
    graph_json = json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
    return graph_json

tamarajqawasmeh
  • 253
  • 1
  • 7