I have the following issue:
I need to create a graph showing the results of an experiment I did in the lab recently. Unfortunately, two of these values are very close together due to an error (which must be considered). When I create the graph, these two values are so close to each other that they are basically unreadable. I am clueless about what I should do because the approaches I found online didn't seem to work for me, as I need to have the exact values on the x-axis. Every marker needs to be placed exactly above the corresponding value and be clearly visible. No matter how much I stretch the graph, I can't get the two values to be readable because they overlap so much.
Is there any way to add some space between the two values or space all values evenly?
Below is a snippet of my code and a picture of the graph:
import matplotlib.pyplot as plt
import numpy as np
def _plot(x, y, titel):
xaxis = np.array(x)
yaxis = np.array(y)
z = np.polyfit(xaxis,yaxis,1)
p = np.poly1d(z)
print("\n\nFormel: y=%.6fx+%.6f"%(z[0],z[1]))
print("Steigung von Trendlinie: {:.3f}".format(z[0]))
plt.scatter(xaxis, yaxis, marker='x', color='red', label='Werte')
plt.plot(xaxis, p(xaxis), marker='x', color='black', mec='b', ls="--")
plt.plot(xaxis, yaxis, color='#875252', ls=':')
plt.tight_layout()
plt.xticks(xaxis, rotation=45, fontsize=8)
plt.xlabel('c(Pb²⁺) in mol/L')
plt.ylabel(r"$\mathrm{pK}_{L}$")
plt.legend(['gemessene Werte', 'Trendlinie / Linearer Fit'])
plt.title(titel)
plt.grid()
plt.show()
I use the _plot function later throughout the program to start the plotting process. The values for x
and y
are both lists of floats. In this specific graph it is: x = [0.4139, 0.2192, 0.2170, 0.1124, 0.0570, 0.0289, 0.0144]
and y = [2.8538, 3.279, 3.2487, 3.6497, 4.0136, 4.3936, 4.6968]
. They are passed into np.array
. The titel
parameter is just any string that's going to be used for plt.title()
.
Thank you in advance for your help! :)
First, I'd suggest using the Axes interface instead of the pyplot
interface.
You could manually move the 0.217 label to the left (inspired by this answer):
import matplotlib.pyplot as plt
from matplotlib.transforms import ScaledTranslation
import numpy as np
x = [0.4139, 0.2192, 0.2170, 0.1124, 0.0570, 0.0289, 0.0144]
y = [2.8538, 3.279, 3.2487, 3.6497, 4.0136, 4.3936, 4.6968]
titel = "Titel"
def _plot(x, y, titel):
xaxis = np.array(x)
yaxis = np.array(y)
z = np.polyfit(xaxis,yaxis,1)
p = np.poly1d(z)
print("\n\nFormel: y=%.6fx+%.6f"%(z[0],z[1]))
print("Steigung von Trendlinie: {:.3f}".format(z[0]))
fig, ax = plt.subplots()
ax.scatter(xaxis, yaxis, marker='x', color='red', label='Werte')
ax.plot(xaxis, p(xaxis), marker='x', color='black', mec='b', ls="--")
ax.plot(xaxis, yaxis, color='#875252', ls=':')
fig.tight_layout()
ax.set_xticks(xaxis, xaxis, rotation=45, fontsize=8)
ax.set_xlabel('c(Pb²⁺) in mol/L')
ax.set_ylabel(r"$\mathrm{pK}_{L}$")
ax.legend(['gemessene Werte', 'Trendlinie / Linearer Fit'])
ax.set_title(titel)
ax.grid()
return ax
ax = _plot(x, y, titel)
# move the 0.217 label slightly to the left
labels = ax.get_xticklabels()
offset = ScaledTranslation(-10/72, 0/72, ax.figure.dpi_scale_trans)
labels[2].set_transform(labels[2].get_transform() + offset)
plt.show()