Matplotlib displays several dark lines along the x axis

Below is the plot. Dark lines cover the x axis, and for some reason it seems like I can't debug this. I am trying to display two columns from a data framework, a 3-dimensional SMA price versus time, as can be seen from the code below.

Picture 1

This is the result of this code.

import datetime

import matplotlib.pyplot as plt
import pandas as pd
from binance.client import Client

BASE = "ETH"
QUOTE = "USDT"
MARKET = BASE + QUOTE

client = Client("vitalik",
                "buterin")

klines = client.get_historical_klines(MARKET, Client.KLINE_INTERVAL_15MINUTE, "31 Dec, 2017", "13 Jan, 2018")

# temporary fix

for kline in klines:
    kline[0] = datetime.datetime.fromtimestamp(
        int(kline[0] / 1000)
    ).strftime('%Y-%m-%d %H:%M:%S')
    kline[6] = datetime.datetime.fromtimestamp(
        int(kline[6] / 1000)
    ).strftime('%Y-%m-%d %H:%M:%S')

df = pd.DataFrame(klines,
                  columns=["open_time", "open", "high", "low", "close", "volume", "close_time", "quote_asset_volume",
                           "trades", "base_volume", "quote_volume", "ignore"])

df['sma_3'] = df["close"].rolling(window=3).mean()

print(df.dtypes)

fig, ax = plt.subplots(figsize=(10, 6))
ax.xaxis_date()

plt.xticks(rotation=90)
plt.xlabel("Date")
plt.ylabel("Price")
plt.title(MARKET)
plt.plot_date(x=df["close_time"], y=df["sma_3"], fmt="r-")
plt.show()

Result print(df.dtypes):

open_time              object
open                   object
high                   object
low                    object
close                  object
volume                 object
close_time             object
quote_asset_volume     object
trades                  int64
base_volume            object
quote_volume           object
ignore                 object
sma_5                 float64
sma_3                 float64
dtype: object

Any help would be greatly appreciated. Thank.

0
source share
2 answers

I think there are too many x tags, so you need to set them to invisible:

spacing = 10
visible = ax.xaxis.get_ticklabels()[::spacing]
for label in ax.xaxis.get_ticklabels():
    if label not in visible:
        label.set_visible(False)

plt.show()
+2
source

You use rows as quantity to plot the x-axis. Therefore, each point will receive its own label. The accumulation of all these labels looks like "dark lines".

, datetime , .

, binance, .strftime('%Y-%m-%d %H:%M:%S') .

+1

Source: https://habr.com/ru/post/1692251/


All Articles