第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 Matplotlib Plot 中獲取 x 軸的恒定間距/寬度時遇到問題?

在 Matplotlib Plot 中獲取 x 軸的恒定間距/寬度時遇到問題?

慕森王 2023-06-13 15:19:05
我有一個名為 Graph 的數(shù)據(jù)框,我在下面附上了我的結(jié)果圖    Date    Central_SMA  Bottom_Central_SMA Top_Central_SMA0   2020-06-02  97.891667   97.7125         98.0708331   2020-06-03  98.833333   98.9250         98.7416672   2020-06-04  98.516667   98.4625         98.5708333   2020-06-05  98.175000   98.0000         98.3500004   2020-06-08  98.633333   98.5000         98.766667下面的代碼將計算圖形,但 x 軸的寬度即在特定日期不是常數(shù)。如何使給定日期的間距保持不變?Graph.reset_index(inplace=True)Graph['Date'] = Graph['Date'].apply(date2num)fig = plt.figure()ax1 = fig.add_subplot(111)ax2 = fig.add_subplot(111)ax3 = fig.add_subplot(111)ax1.xaxis_date()ax1.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))ax2.plot(Graph.Date, Graph['Central_SMA'], label='Central SMA',ls='steps')ax3.plot(Graph.Date, Graph['Top_Central_SMA'], label='Top SMA',ls='steps')import matplotlibmatplotlib.rc('figure', figsize=[100,20])plt.show()
查看完整描述

1 回答

?
慕俠2389804

TA貢獻1719條經(jīng)驗 獲得超6個贊

一種方法是將日期設(shè)置為索引。然后,執(zhí)行reindex()從第一個到最后一個日期的所有日期。這將填寫'NaN'(“不是數(shù)字”或“不可用”)對應(yīng)于缺失日期的數(shù)據(jù)。NaN值通常會在繪制的數(shù)據(jù)中創(chuàng)建一個空白點。


我不知道ls='steps'matplotlib 中的選項plot(),但有一個類似的選項step()可以創(chuàng)建步驟圖。(在 matplotlib 之上,pandas 和 seaborn 還構(gòu)建了一些接口來創(chuàng)建這種和許多其他類型的繪圖。)


順便說一句,當在同一個地方繪制多個圖形時,重復(fù)使用同一個圖形通常效果最好ax。plt.subplots()是通過一次調(diào)用創(chuàng)建圖窗和子圖(默認 1 行 1 列)的簡便方法。可以設(shè)置許多選項,其中figsize.


import pandas as pd

from matplotlib import pyplot as plt

from matplotlib import dates as mdates

from io import StringIO


data_str = '''    Date    Central_SMA  Bottom_Central_SMA Top_Central_SMA

0   2020-06-02  97.891667   97.7125         98.070833

1   2020-06-03  98.833333   98.9250         98.741667

2   2020-06-04  98.516667   98.4625         98.570833

3   2020-06-05  98.175000   98.0000         98.350000

4   2020-06-08  98.633333   98.5000         98.766667'''

Graph = pd.read_csv(StringIO(data_str), delim_whitespace=True)

Graph['Date'] = pd.to_datetime(Graph['Date'])  # just making sure the 'Date' really is in pandas date format

Graph.set_index('Date', inplace=True)

Graph = Graph.reindex(index=pd.date_range(start=Graph.index[0], end=Graph.index[-1], freq='D'))


fig, ax = plt.subplots(figsize=(12, 5))


ax.step(Graph.index, Graph['Central_SMA'], label='Central SMA')

ax.step(Graph.index, Graph['Top_Central_SMA'], label='Top SMA')

ax.xaxis_date()

ax.xaxis.set_major_formatter(mdates.DateFormatter('%d-%m-%Y'))

ax.legend()

plt.show()

http://img1.sycdn.imooc.com//648818960001687511370417.jpg

查看完整回答
反對 回復(fù) 2023-06-13
  • 1 回答
  • 0 關(guān)注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號