Very strange horizontal lines in plt graph

Viewed 48

I have a following code that plots three lines into one graph in matplotlib:


import pandas as pd
import matplotlib.pylab as plt



df = pd.read_csv("balici_naskladnovaci.csv")

df = df.sort_values(by = "interval start")
df["interval start"] = pd.to_datetime(df["interval start"])

# balici
fig, ax = plt.subplots()

ax.plot(
    df[df["expedice"] == "liberec"]["interval start"].astype(str),
    df[df["expedice"] == "liberec"]["balici"],
    color="red",
    label="Liberec",
)

ax.plot(
    df[df["expedice"] == "jablonec"]["interval start"].astype(str),
    df[df["expedice"] == "jablonec"]["balici"],
    color="blue",
    label="Jablonec",
)

ax.plot(
    df[df["expedice"] == "lipa"]["interval start"].astype(str),
    df[df["expedice"] == "lipa"]["balici"],
    color="green",
    label="Lípa",
)

ax.set_ylabel("Počet baličů", color="black", fontsize=14)


plt.title("Balici napric expedicemi")
plt.tight_layout()
fig.legend()
plt.show()

But the result is very strange, because of these horizontal lines:

enter image description here

Do you know, how can I fix it please?

1 Answers

Here is my own solution, seaborn seems to be much more efficient in this type of data:



ax = plt.axes()

sns.lineplot(x ='interval start', y = 'balici', hue='expedice',  data=df)


for tick in ax.get_xticklabels():
    tick.set_rotation(90)

for index, label in enumerate(ax.xaxis.get_ticklabels()):
    label.set_visible(False)
    if index % 10 == 0:
        label.set_visible(True)

plt.title("Balici napric expedicemi")
plt.tight_layout()
plt.show()

Related