[英]Bitcoin Chart with log scale Python
I'm using Python (beginner) and I want to plot the Bitcoin price in log scale but without seeing the log price, I want to see the linear price.我正在使用 Python(初学者),我想要 plot 的对数比特币价格,但没有看到对数价格,我想看到线性价格。
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
from cryptocmd import CmcScraper
from math import e
from matplotlib.ticker import ScalarFormatter
# -------------IMPORT THE DATA----------------
btc_data = CmcScraper("BTC", "28-04-2012", "27-11-2022", True, True, "USD")
# Create a Dataframe
df = btc_data.get_dataframe()
#Set the index as Date instead of numerical value
df = df.set_index(pd.DatetimeIndex(df["Date"].values))
df
#Plot the Data
plt.style.use('fivethirtyeight')
plt.figure(figsize =(20, 10))
plt.title("Bitcoin Price", fontsize=18)
plt.yscale("log")
plt.plot(df["Close"])
plt.xlabel("Date", fontsize=15)
plt.ylabel("Price", fontsize=15)
plt.show()
As you can see we have log scale price but I want to see "100 - 1 000 - 10 000" instead of "10^2 - 10^3 - 10^4" on the y axis.如您所见,我们有对数刻度价格,但我想在 y 轴上看到“100 - 1 000 - 10 000”而不是“10^2 - 10^3 - 10^4”。
Does anyone know how to solve this?有谁知道如何解决这个问题?
Have a nice day!祝你今天过得愉快!
Welcome to Stackoverflow!欢迎来到 Stackoverflow!
You were getting there, the following code will yield what you want (I simply added some fake data + 1 line of code to your plotting code):你到了那里,下面的代码将产生你想要的(我只是在你的绘图代码中添加了一些假数据 + 1 行代码):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
y = [10**x for x in np.arange(0, 5, 0.1)]
x = [x for x in np.linspace(2018, 2023, len(y))]
#Plot the Data
plt.style.use('fivethirtyeight')
plt.figure(figsize =(20, 10))
plt.title("Bitcoin Price", fontsize=18)
plt.yscale("log")
plt.plot(x, y)
plt.xlabel("Date", fontsize=15)
plt.ylabel("Price", fontsize=15)
plt.gca().get_yaxis().set_major_formatter(ticker.ScalarFormatter())
plt.show()
This generates the following figure:这会生成下图:
The fundamental lines are these:基本线是这些:
import matplotlib.ticker as ticker
plt.gca().get_yaxis().set_major_formatter(ticker.ScalarFormatter())
Explanation: plt.gca()
gets the currently active axis object. This object is the one we want to adapt.说明:
plt.gca()
得到当前激活的轴object,这个object就是我们要适配的。 And the actual thing we want to adapt is the way our ticks get formatted for our y axis.我们真正想要调整的是我们的刻度为我们的 y 轴格式化的方式。 Hence the latter part:
.get_yaxis().set_major_formatter()
.因此,后一部分:
.get_yaxis().set_major_formatter()
。 Now, we only need to choose which formatter.现在,我们只需要选择格式化程序。 I chose ScalarFormatter, which is the default for scalars.
我选择了 ScalarFormatter,这是标量的默认值。 More info on your choices can be found here .
可以在此处找到有关您的选择的更多信息。
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.