import matplotlib.pyplot as plt
import numpy as np

# --- Original data ---
x = np.array([0, 0.5, 1.0, 2.1, 3.5])
values = np.array([0.02614146539, 0.0223338683, 0.0176443878, 0.0148875675, 0.0156582799])
errors = np.array([0.70493295, 1.637198533, 1.373243502, 4.309374753, 1.987814535]) / 1000

# New data for second y-axis
values2 = np.array([9.046666667, 7.946666667, 7.803333333, 7.096666667, 6.923333333])
errors2 = np.array([0.032145503, 0.083864971, 0.096090235, 0.223681321, 0.087368949])

# --- Style tweaks ---
plt.rcParams.update({
    "font.family": "serif",
    "font.size": 12,
    "axes.labelsize": 12,
    "xtick.labelsize": 11,
    "ytick.labelsize": 11,
    "axes.spines.top": False,
    "axes.spines.right": False,  # We will add secondary axis manually
    "axes.linewidth": 1,
})

fig, ax = plt.subplots(figsize=(5, 4))

# --- Primary data series ---
ax.errorbar(
    x, values,
    yerr=errors,
    fmt='o',
    markersize=6,
    markerfacecolor='white',
    markeredgecolor='black',
    capsize=4,
    linewidth=1)

ax.set_xlabel("$p_\\mathrm{0,CO_2}$ / bar")
ax.set_ylabel("$C_\\mathrm{U}$(60 min) / mol L$^{-1}$")
ax.set_xlim(-0.1, 3.6)
ax.set_ylim(5/1000, 35/1000)
ax.set_xticks([0, 0.5,1, 1.5, 2,2.5, 3, 3.5])
ax.set_yticks([5/1000, 10/1000, 15/1000, 20/1000, 25/1000, 30/1000, 35/1000, 40/1000])
ax.yaxis.grid(False)
ax.set_axisbelow(True)

# Make all spines visible
for spine in ax.spines.values():
    spine.set_visible(True)
    spine.set_linewidth(1.1)

# --- Secondary y-axis ---
ax2 = ax.twinx()  # create second y-axis sharing x-axis
ax2.errorbar(
    x, values2,
    yerr=errors2,
    fmt='o',
    markersize=6,
    markerfacecolor='steelblue',
    markeredgecolor='steelblue',
    capsize=4,
    linewidth=1)


ax2.set_ylabel("pH / -", color='steelblue')
ax2.tick_params(axis='y', colors='steelblue')
ax2.set_ylim(3, 10)  # Adjust range for second data

# Optional: add legends for both axes
lines1, labels1 = ax.get_legend_handles_labels()
lines2, labels2 = ax2.get_legend_handles_labels()

plt.tight_layout()

plt.savefig("data.pdf", format='pdf', bbox_inches='tight')

plt.show()