Python 使用点(而不是水平线)和更精细的y轴值修改打印
问题:Python 使用点(而不是水平线)和更精细的y轴值修改打印,python,pandas,matplotlib,Python,Pandas,Matplotlib,问题: import matplotlib.pyplot as plt import matplotlib.ticker as ticker df = pd.read_csv('for_stack_nums.csv') axnum = df[['High','Low']].plot() axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f')) for idx, l in df.nlargest(5, '# of
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df = pd.read_csv('for_stack_nums.csv')
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
for idx, l in df.nlargest(5, '# of Trades').iterrows():
plt.axhline(y=l['High'], color='r')
plt.axhline(y=l['Low'], color='b')
plt.show()
使用此数据和以下代码创建一个曲线图,显示'of Trades'
列中最大值的水平线
我们如何将这些nlargest
值显示为一个点(散点图是正确的术语吗?),该点位于观察到nlargest
值的区域,而不是水平线
数据:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df = pd.read_csv('for_stack_nums.csv')
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
for idx, l in df.nlargest(5, '# of Trades').iterrows():
plt.axhline(y=l['High'], color='r')
plt.axhline(y=l['Low'], color='b')
plt.show()
所需输出:
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
df = pd.read_csv('for_stack_nums.csv')
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
for idx, l in df.nlargest(5, '# of Trades').iterrows():
plt.axhline(y=l['High'], color='r')
plt.axhline(y=l['Low'], color='b')
plt.show()
显示现有的df[['High','Low']].plot()
的绘图,但水平线替换为点,y轴值以.05的步长递增,而不是当前看到的.2。
如果点的大小可以是相对于最大值的排名,这将是很好的。所以最大的值就是最大的点。根据注释修改答案
您需要创建散点图。下面是一些简单的代码,可以帮您完成
import matplotlib.ticker as ticker
axnum = df[['High','Low']].plot()
axnum.yaxis.set_major_formatter(ticker.FormatStrFormatter('%.2f'))
axnum.yaxis.set_major_locator(ticker.MultipleLocator(.05))
x = np.linspace(0, len(df), 10)
for i, (idx, l) in enumerate(df.nlargest(5, '# of Trades').iterrows()):
plt.scatter(x, y=[l['High']]*len(x), color='r', marker='o', s=(5-i) * 5 + 5)
plt.scatter(x, y=[l['Low']]*len(x), color='b', marker='o', s=(5-i) * 5 + 5)
散射是您要寻找的,可以将点放置在观察到最大值的区域。那么,在各自的时间戳上,每个最大值对应一个点?许多人更新了答案,只在观察到的点上绘制点。