Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 23131

Using matplotlib and pandas to make a bar chart

$
0
0

enter image description here

I have a bunch of data that I am trying to plot as a bar chart for each lithology. I would like the Y-axis to be the percent of each RQD Bin for each lithology, rather than the total length. I know that the current "groupby" is not going to work as it is currently summing the length, but I am not familiar with what to update to fix this. Any help would be greatly appreciated!

here is my current code...

    # Define ranges and corresponding names    ranges = [(0, 10),         (10.00001, 20),           (20.00001, 30),           (30.00001, 40),           (40.00001, 50),           (50.00001, 60),           (60.00001, 70),           (70.00001, 80),           (80.0001,90),           (90.00001, 1000)]    names = ["0 to 10", "10 to 20", "20 to 30", "30 to 40", "40 to 50", "50 to 60", "60 to 70", "70 to 80", "80 to 90", "90 to 100"]    # Function to assign names based on ranges    def assign_name(value):      for low, high in ranges:        if low <= value <= high:          return names[ranges.index((low, high))]      return None  # Handle values outside all ranges    # Add a new column based on RQD ranges    df['RQD_Bin'] = df['RQD'].apply(assign_name)    df["Length"] = df["To"] - df["From"]    # Group data by lithology and calculate counts for each RQD_Bin    lithology_distr = df.groupby(['Lithology', 'RQD_Bin'])['Length'].sum().unstack()    # Create a bar chart    lithology_distr.plot(kind='bar', stacked=False)  # Set stacked=False for separate bars    # Customize the plot for better readability    plt.xlabel('RQD (%)')    plt.ylabel('Total Length Contribution')  # Update y-axis label    plt.title('RQD by Lithology')    plt.legend(title='RQD Bin')    plt.xticks(rotation=90)  # Rotate x-axis labels to prevent overlapping    # Display the chart and plot    plt.savefig("RQD_Distribution.png")  # Replace with ".jpg" or other format if needed    plt.show()

Viewing all articles
Browse latest Browse all 23131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>