I'm working on a code that should generate a Cleveland Dot Plot, got to this point so far:
import matplotlib.pyplot as plt
import numpy as np
from typing import List, T
from random import uniform
class ClevelandDotPlotter:
def __init__(self,
ax: plt.Axes,
data: List[List[float]],
colors: List[str],
size: float,
x_labels: List[T],
x_labels_size: float,
x_labels_color: str,
y_labels: List[T],
y_labels_size: float,
y_labels_color: str,
line_color: str,
line_width: float,
line_style: str,
line_alpha: float,
title: str,
title_color: str,
title_size: float):
self.ax = ax
self.data = data
self.colors = colors
self.size = size
self.x_labels = x_labels
self.x_labels_size = x_labels_size
self.x_labels_color = x_labels_color
self.y_labels = y_labels
self.y_labels_size = y_labels_size
self.y_labels_color = y_labels_color
self.line_color = line_color
self.line_width = line_width
self.line_style = line_style
self.line_alpha = line_alpha
self.title = title
self.title_color = title_color
self.title_size = title_size
self.x_coords = np.linspace(0, 1, max((len(i) for i in self.data)),
True)
print(self.x_coords)
def dot_plot(self):
t_data = list(map(list, zip(*self.data)))
for x, data in zip(self.x_coords, t_data):
(self.ax).axvline(x, color = self.line_color,
ls = self.line_style,
lw = self.line_width,
alpha = self.line_alpha)
(self.ax).scatter([x] * len(data), data, self.size, self.colors)
def main():
fig, ax = plt.subplots(figsize = (10, 5))
plot = ClevelandDotPlotter(ax, [[uniform(0, 1) for _ in range(10)] for _ in range(2)],
["#fa4d56", "#1192e8"], 15, [], 16, "black",
[], 16, "black", "black", 1, "dashed", 0.33, "hey",
"black", 18)
plot.dot_plot()
plt.savefig("test0.png")
if __name__ == "__main__":
main()
My problem is that the vertical lines are off from the dots by literally some pixels, and I have no clue how to fix this. How can I align the lines properly?