I'm trying to create a scatterplot for a bunch of probability values for two labels, but when I plot it, the labels appear left- and right-justified so there's a bunch of empty space in-between. Is there a way to narrow the gap between the two x-axis tick marks?
Here's the code I used:
x = [1,2]
y = [[0.1, 0.6, 0.9],[0.5,0.7,0.8]]
colors = ['magenta', 'blue']
plt.title("Algorithm comparison - p-values")
for xe, ye,c in zip(x, y,colors):
plt.scatter([xe] * len(ye), ye, c=c)
plt.xticks([1,2])
plt.axes().set_xticklabels(['Part 1','Part 2'],rotation = 45)
Thanks in advance! Please let me know if I left out any important details.
CodePudding user response:
I have rewritten the code as closely as possible. The main problem is that you generate two axes objects - one with for xe, ye,c in zip(x, y,colors): plt.scatter([xe] * len(ye), ye, c=c) and another one with plt.axes().set_xticklabels(['Part 1','Part 2'],rotation = 45). You can provide the labels instead directly with plt.xticks():
import matplotlib.pyplot as plt
x = [1,2]
y = [[0.1, 0.6, 0.9],[0.5,0.7,0.8]]
colors = ['red', 'blue']
plt.title("Algorithm comparison - p-values")
for xe, ye,c in zip(x, y,colors):
plt.scatter([xe] * len(ye), ye, c=c)
plt.xticks([1,2], ['Part 1','Part 2'], rotation = 45)
plt.show()
Preferably, you create one axis object in the beginning and plot everything using this axis object:
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
x = [1,2]
y = [[0.1, 0.6, 0.9],[0.5,0.7,0.8]]
colors = ['red', 'blue']
ax.set_title("Algorithm comparison - p-values")
for xe, ye, c in zip(x, y, colors):
ax.scatter([xe] * len(ye), ye, c=c)
ax.set_xticks(x)
ax.set_xticklabels(['Part 1','Part 2'],rotation = 45)
ax.set_xlim(0.5, 2.5)
plt.show()
For more explanations regarding differences between the object-oriented approach and the plt interface can be found here and the matplotlib documentation.

