Last active
August 12, 2022 14:39
-
-
Save luisdamed/9aca7a80cb69ad8a8a5cdd814ce2a378 to your computer and use it in GitHub Desktop.
Compare histograms of different data series by creating bar charts and adding some descriptive statistics to the figure
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.read_csv('test_FSCar_ddMMYYYY_TrackN_setupID2.csv') | |
# Get statistics | |
meanFL = df.trq_NmFL.mean() | |
meanFR = df.trq_NmFR.mean() | |
meanRL = df.trq_NmRL.mean() | |
meanRR = df.trq_NmRR.mean() | |
stdFL = df.trq_NmFL.std() | |
stdFR = df.trq_NmFR.std() | |
stdRL = df.trq_NmRL.std() | |
stdRR = df.trq_NmRR.std() | |
# Prepare text to display | |
txt = ( | |
f"mean FL = {meanFL:.1f} Nm\n" | |
f"mean FR = {meanFR:.1f} Nm\n" | |
f"mean RL = {meanRL:.1f} Nm\n" | |
f"mean RR = {meanRR:.1f} Nm\n\n" | |
f"stdev FL = {stdFL:.1f} Nm\n" | |
f"stdev FR = {stdFR:.1f} Nm\n" | |
f"stdev RL = {stdRL:.1f} Nm\n" | |
f"stdev RR = {stdRR:.1f} Nm\n") | |
# Create figure | |
fig = plt.figure(figsize = (2550/600, 1500/600), dpi=600) | |
ax = fig.add_subplot() | |
plt.hist([df.trq_NmFL, df.trq_NmFR, df.trq_NmRL, df.trq_NmRR], | |
bins = 15, | |
label = ['trq_NmFL', 'trq_NmFR', 'trq_NmRL', 'trq_NmRR'] , | |
rwidth=0.7, | |
edgecolor='black', linewidth=.5) | |
# Format | |
plt.suptitle('Histogram with Python') | |
plt.xlabel('Torque [Nm]', fontsize = 8) | |
plt.xticks(fontsize = 8) | |
plt.ylabel('Counts', fontsize = 8) | |
plt.yticks(fontsize = 8) | |
plt.grid(b=True, which='both', color='k', linestyle='--', alpha=0.2) | |
plt.legend(loc='upper right', fontsize = 4) | |
# Add text and display figure | |
ax.text(100, 25000, txt, fontsize=5) | |
fig.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment