Python Matplotlib设置xaxis上的频率
我想用以下代码设置图表x轴的频率:Python Matplotlib设置xaxis上的频率,python,matplotlib,Python,Matplotlib,我想用以下代码设置图表x轴的频率: fig, ax = plt.subplots() fig = plt.figure(figsize=(4,3)) plt.plot(vol['BTC'], 'b-', vol['ETH'], 'r-', vol['XRP'], 'g-', vol['BCH'], 'y-') plt.legend(labels= ['BTC','ETH','XRP','BCH'], fontsize="xx-small") plt.xticks(rotation=30, hor
fig, ax = plt.subplots()
fig = plt.figure(figsize=(4,3))
plt.plot(vol['BTC'], 'b-', vol['ETH'], 'r-', vol['XRP'], 'g-', vol['BCH'], 'y-')
plt.legend(labels= ['BTC','ETH','XRP','BCH'], fontsize="xx-small")
plt.xticks(rotation=30, horizontalalignment='right')
start, end = ax.get_xlim()
ax.xaxis.set_ticks(np.arange(start, end, 20))
plt.rcParams.update({'font.size': 3})
然而,没有任何变化,当标签重叠时,它仍然像一大块墨水。
我想降低显示频率,缩小标签的尺寸。
任何帮助都将不胜感激
编辑:
我尝试了以下方法,但图表是空的
My new code:
years = mdates.YearLocator() # every year
months = mdates.MonthLocator() # every month
days = mdates.DayLocator() # every day
years_fmt = mdates.DateFormatter('%Y-%m-%d')
vol_index = vol.index
fig, ax = plt.subplots()
ax.plot(np.array(vol.index), np.array(vol['BTC']))
# format the ticks
ax.xaxis.set_major_locator(years)
ax.xaxis.set_major_formatter(years_fmt)
ax.xaxis.set_minor_locator(days)
# round to nearest years.
datemin = pd.to_datetime(np.datetime64(vol.index[0], 'D')).strftime('%Y-%m-%d')
datemax = pd.to_datetime(np.datetime64(vol.index[-1], 'D') + np.timedelta64(1, 'D')).strftime('%Y-%m-%d')
ax.set_xlim(datemin, datemax)
# format the coords message box
ax.format_xdata = mdates.DateFormatter('%Y-%m-%d')
ax.format_ydata = lambda x: '$%1.2f' % x # format the price.
ax.grid(True)
fig.autofmt_xdate()
此代码使用以下输入:
np.array(vol['BTC'])
:
vol.index
:
Index(['2019-11-01 23:59:08', '2019-11-02 23:59:08', '2019-11-03 23:59:04',
'2019-11-04 23:59:12', '2019-11-05 23:59:03', '2019-11-06 23:59:08',
'2019-11-07 23:59:04', '2019-11-08 23:59:07', '2019-11-09 23:59:06',
'2019-11-10 23:59:05', '2019-11-11 23:59:03', '2019-11-12 23:59:04',
'2019-11-13 23:59:06', '2019-11-14 23:59:06', '2019-11-15 23:59:05',
'2019-11-16 23:59:12', '2019-11-17 23:59:03', '2019-11-18 23:59:10',
'2019-11-19 23:59:09', '2019-11-20 23:59:06', '2019-11-21 23:59:11',
'2019-11-22 23:59:05', '2019-11-23 23:59:06', '2019-11-24 23:59:09',
'2019-11-25 23:59:05', '2019-11-26 23:59:03', '2019-11-27 23:59:04',
'2019-11-28 23:59:05', '2019-11-29 23:59:04', '2019-11-30 23:59:02',
'2019-12-01 23:59:06', '2019-12-02 23:59:11', '2019-12-03 23:59:02',
'2019-12-04 23:59:07', '2019-12-05 23:59:07', '2019-12-06 23:59:08',
'2019-12-07 23:59:07', '2019-12-08 23:59:06', '2019-12-09 23:59:06',
'2019-12-10 23:59:06', '2019-12-11 23:59:11', '2019-12-12 23:59:05',
'2019-12-13 23:59:04', '2019-12-14 23:59:08', '2019-12-15 23:59:07',
'2019-12-16 23:59:13', '2019-12-17 23:59:10', '2019-12-18 23:59:12',
'2019-12-19 23:59:09', '2019-12-20 23:59:10', '2019-12-21 23:59:06',
'2019-12-22 23:59:07', '2019-12-23 23:59:07', '2019-12-24 23:59:13',
'2019-12-25 23:59:10', '2019-12-26 23:59:11', '2019-12-27 23:59:04',
'2019-12-28 23:59:05', '2019-12-29 23:59:06', '2019-12-30 23:59:07',
'2019-12-31 23:59:09', '2020-01-01 23:59:06', '2020-01-02 23:59:02',
'2020-01-03 23:59:06', '2020-01-04 23:59:03', '2020-01-05 23:59:08',
'2020-01-06 23:59:07', '2020-01-07 23:59:08', '2020-01-08 23:59:06',
'2020-01-09 23:59:03', '2020-01-10 23:59:10', '2020-01-11 23:59:02',
'2020-01-12 23:59:07', '2020-01-13 23:59:04', '2020-01-14 23:59:07',
'2020-01-15 23:59:04', '2020-01-16 23:59:06', '2020-01-17 23:59:03',
'2020-01-18 23:59:09', '2020-01-19 23:59:04', '2020-01-20 23:59:04',
'2020-01-21 23:59:08', '2020-01-22 23:59:04'],
dtype='object', name='timestamp')
vol
:
BTC ETH XRP BCH
timestamp
2019-11-01 23:59:08 0.054987 0.040244 0.029715 0.082026
2019-11-02 23:59:08 0.034541 0.029485 0.026469 0.078933
2019-11-03 23:59:04 0.034541 0.027006 0.026571 0.057731
2019-11-04 23:59:12 0.022937 0.027006 0.026571 0.052057
2019-11-05 23:59:03 0.021911 0.026792 0.023252 0.040931
... ... ... ... ...
2020-01-18 23:59:09 0.046494 0.086220 0.066950 0.162209
2020-01-19 23:59:04 0.048259 0.084283 0.066950 0.157251
2020-01-20 23:59:04 0.045850 0.081700 0.064228 0.149919
2020-01-21 23:59:08 0.027414 0.041858 0.042955 0.092721
2020-01-22 23:59:04 0.027414 0.041858 0.042955 0.092721
您需要一个:
如果需要,您可以这样做,而不是修改rcParams:
ax.tick_params(axis='x',which='major',labelsize=3)
您需要一个:
如果需要,您可以这样做,而不是修改rcParams:
ax.tick_params(axis='x',which='major',labelsize=3)
这是另一种方法,您可以指定所需的节拍数而不是频率,但您可能仍然会发现它很有用:
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
x=np.arange('2017-01','2020-01',dtype='datetime64[D]'))
y=np.random.random(len(x))
df=pd.DataFrame({'x':x,'y':y})
plt.散射(df['x']值,df['y']值)
ticks=np.arange(x[0],x[-1],(x[-1]-x[0]).astype('timedelta64[D]')/15)
plt.xticks(滴答声,滴答声)
plt.gca().tick_参数('x',labelrotation=45,labelsize=10)
plt.show()
给
而不是
编辑 这是一个使用给定数据的完整示例
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
将numpy作为np导入
BTC=np.数组([0.05498711,0.03454131,0.03454131,0.02293731,0.02191053,0.02191053,
0.01688648,0.02828462,0.02828462,0.02828462,0.03188105,0.03054535,
0.02955679,0.0270898, 0.0281818, 0.02866918,0.02710031,0.02668099,
0.03404319,0.03670357,0.05311734,0.08663729,0.08663729,0.08339912,
0.08831546,0.08063601,0.07522458,0.06043395,0.06280006,0.06543079,
0.06543079,0.0444152, 0.0444152, 0.03575803,0.03575803,0.03575803,
0.02870328,0.02870328,0.02870328,0.02870328,0.02835626,0.02263508,
0.02263508,0.02514882,0.0332816, 0.02463769,0.0385677, 0.0427951,
0.04329788,0.04329788,0.04329788,0.05240083,0.05883257,0.05883257,
0.03162634,0.02906886,0.02765406,0.024522, 0.02352016,0.01756594,
0.02030382,0.02030382,0.03326152,0.03601441,0.03601441,0.03250479,
0.04571088,0.06313575,0.07639834,0.07639834,0.0555884, 0.05133794,
0.0493664, 0.03279575,0.05379169,0.05431873,0.05431873,0.04422756,
0.04649411,0.04825915,0.04584981,0.02741382,0.02741382])
idx=np.数组(['2019-11-01 23:59:08','2019-11-02 23:59:08','2019-11-03 23:59:04',',
'2019-11-04 23:59:12', '2019-11-05 23:59:03', '2019-11-06 23:59:08',
'2019-11-07 23:59:04', '2019-11-08 23:59:07', '2019-11-09 23:59:06',
'2019-11-10 23:59:05', '2019-11-11 23:59:03', '2019-11-12 23:59:04',
'2019-11-13 23:59:06', '2019-11-14 23:59:06', '2019-11-15 23:59:05',
'2019-11-16 23:59:12', '2019-11-17 23:59:03', '2019-11-18 23:59:10',
'2019-11-19 23:59:09', '2019-11-20 23:59:06', '2019-11-21 23:59:11',
'2019-11-22 23:59:05', '2019-11-23 23:59:06', '2019-11-24 23:59:09',
'2019-11-25 23:59:05', '2019-11-26 23:59:03', '2019-11-27 23:59:04',
'2019-11-28 23:59:05', '2019-11-29 23:59:04', '2019-11-30 23:59:02',
'2019-12-01 23:59:06', '2019-12-02 23:59:11', '2019-12-03 23:59:02',
'2019-12-04 23:59:07', '2019-12-05 23:59:07', '2019-12-06 23:59:08',
'2019-12-07 23:59:07', '2019-12-08 23:59:06', '2019-12-09 23:59:06',
'2019-12-10 23:59:06', '2019-12-11 23:59:11', '2019-12-12 23:59:05',
'2019-12-13 23:59:04', '2019-12-14 23:59:08', '2019-12-15 23:59:07',
'2019-12-16 23:59:13', '2019-12-17 23:59:10', '2019-12-18 23:59:12',
'2019-12-19 23:59:09', '2019-12-20 23:59:10', '2019-12-21 23:59:06',
'2019-12-22 23:59:07', '2019-12-23 23:59:07', '2019-12-24 23:59:13',
'2019-12-25 23:59:10', '2019-12-26 23:59:11', '2019-12-27 23:59:04',
'2019-12-28 23:59:05', '2019-12-29 23:59:06', '2019-12-30 23:59:07',
'2019-12-31 23:59:09', '2020-01-01 23:59:06', '2020-01-02 23:59:02',
'2020-01-03 23:59:06', '2020-01-04 23:59:03', '2020-01-05 23:59:08',
'2020-01-06 23:59:07', '2020-01-07 23:59:08', '2020-01-08 23:59:06',
'2020-01-09 23:59:03', '2020-01-10 23:59:10', '2020-01-11 23:59:02',
'2020-01-12 23:59:07', '2020-01-13 23:59:04', '2020-01-14 23:59:07',
'2020-01-15 23:59:04', '2020-01-16 23:59:06', '2020-01-17 23:59:03',
'2020-01-18 23:59:09', '2020-01-19 23:59:04', '2020-01-20 23:59:04',
'2020-01-21 23:59:08','2020-01-22 23:59:04'],dtype='datetime64[s]')
df=pd.DataFrame({'BTC':BTC})
df.index=pd.DatetimeIndex(idx)
x=df.index.values.astype('datetime64[D]'))
y=df['BTC']
plt.图(figsize=(8,4))
平面图(x,y)
ticks=np.arange(x[0],x[-1],(x[-1]-x[0]).astype('timedelta64[D]')/15)
plt.xticks(滴答声,滴答声)
plt.gca().tick_参数('x',labelrotation=90,labelsize=10)
plt.show()
这会给你
这是一种替代方法,您可以指定所需的节拍数,而不是频率,但您可能仍会发现它很有用:
将熊猫作为pd导入
将matplotlib.pyplot作为plt导入
x=np.arange('2017-01','2020-01',dtype='datetime64[D]'))
y=np.random.random(len(x))
df=pd.DataFrame({'x':x,'y':y})
plt.散射(df['x']值,df['y']值)
ticks=np.arange(x[0],x[-1],(x[-1]-x[0]).astype('timedelta64[D]')/15)
plt.xticks(滴答声,滴答声)
plt.gca().tick_参数('x',labelrotation=45,labelsize=10)
plt.show()
给
而不是
ax.tick_params(axis='x',which='major',labelsize=3)