[Python] Seaborn scatterplot inverts y-axis on some plots?

Discussão em 'Python' iniciado por Stack, Maio 1, 2025.

  1. Stack

    Stack Membro Participativo

    I have two sets of data that I am using to create scatterplots. I use a function to create the plot for each one. For some reason, the plots for one of the sets displays with the minimum y value at the top of the plot (a flipped image, but the text is not inverted). The other one displays as expected.

    The data are read in from Excel files, and are essentially the same. As I said, I call the same functions for each data set.

    The code that draws the plot is:

    from scipy.stats.distributions import chi2
    import matplotlib.pyplot as plt
    import seaborn as sns
    import numpy as np

    def mcbf_plot(df, alpha=0.8):
    xval = list(range(len(df)))
    xval_l = [item-0.1 for item in xval]
    xval_r = [item+0.1 for item in xval]
    xlabs = list(df['venue'])
    g = sns.scatterplot(data=df, x=xval, y='mcbof_pe', s=100, color='k')
    plt.plot([xval,xval], [df['ci_mcbof_l'],df['ci_mcbof_h']], color='k')
    plt.plot([xval_l, xval_r], [df['ci_mcbof_l'],df['ci_mcbof_l']],color='k')
    plt.plot([xval_l, xval_r], [df['ci_mcbof_h'],df['ci_mcbof_h']],color='k')
    plt.xticks(np.arange(0, len(df), 1.0))
    plt.title('Results by Event')
    plt.xlabel('Event')
    plt.ylabel('Cycles')
    g = g.set_xticklabels(labels=xlabs,rotation=45)


    This is my script that I run to generate the plot:

    %matplotlib inline
    import os
    import glob
    from pandas import DataFrame, Series
    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    import seaborn as sns
    import datetime
    import binom.binom as bn
    import matplotlib.dates as mdates

    hist_ram = pd.read_excel('c:/User/Documents/RAM/historic_ram.xlsx')

    bn.mcbf_plot(hist_ram, 0.8)


    And this is an example of the data:

    venue miles events fails mmbof_pe ci_mmbof_l ci_mmbof_h mcbof_pe \
    0 a 1749.8 416 5 349.96 188.664 719.315 83.2
    1 b 353.3 47 14 25.2357 17.5527 37.3088 3.35714
    2 c 1610.1 288 7 230.014 136.786 413.401 41.1429
    3 d 549.1 83 0 None 341.175 None None

    ci_mcbof_l ci_mcbof_h
    0 44.8533 171.011
    1 2.33505 4.96324
    2 24.4671 73.9454
    3 51.5708 None


    Any idea why seaborn/matplotlib would invert one plot but not both (or neither)?

    Edited to add: the None is what is causing the problem. Is there another way to indicate that there is no value for a given cell in the data set? Basically, I want to plot the point estimate and confidence interval for tests with failures, but only the lower confidence limit for a test with no failures. The data treats the LCL as the LCI value, and sets the PE and UCI to None.

    Continue reading...

Compartilhe esta Página