Matplotlib在x轴上绘制多条黑线 [英] Matplotlib plots multiple dark lines on x axis
本文介绍了Matplotlib在x轴上绘制多条黑线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是相关的情节.黑线覆盖了x轴,由于某种原因,我似乎无法调试它.我试图绘制一个数据帧中的两列,这是三个时期的SMA价格与时间的关系,如下面的代码所示.
The following is the plot in question. Dark lines are covering the x-axis, and for some reason it seems I cant debug this. Im trying to chart two columns from a dataframe, the 3 period SMA price against time as can be seen in the code below.
这是此代码的结果
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()
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 highly appreciated. Thank you.
推荐答案
我认为x标签太多,因此需要将它们设置为不可见:
I think there is too many x labels, so need set them to not visible:
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()
这篇关于Matplotlib在x轴上绘制多条黑线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文