I would like to set the frequency of the x axis of my chart with the following code:
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})
However, nothing changes and it remains like a bulk of ink as the labels overlaps. I would like to lower the frequency of display and reduce the size of the labels. Any help would be appreciated.
EDIT: I tried the following but the chart is empty.
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()
This code use the following input:
np.array(vol['BTC'])
:
[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]
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
You need a tick locator :
import matplotlib.ticker as ticker
# ...
ax.xaxis.set_major_locator(ticker.MultipleLocator(20))
And you can do this, if you want, instead of modifying the rcParams:
ax.tick_params(axis='x',which='major',labelsize=3)
This is an alternative approach where you specify the number of ticks you want instead of the frequency, but you may still find it useful:
import pandas as pd
import matplotlib.pyplot as 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.scatter(df['x'].values, df['y'].values)
ticks = np.arange(x[0], x[-1], (x[-1] - x[0]).astype('timedelta64[D]') / 15)
plt.xticks(ticks, ticks)
plt.gca().tick_params('x', labelrotation=45, labelsize=10)
plt.show()
Which gives
instead of
Here is a complete example which uses the given data,
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
BTC = np.array([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.array(['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.figure(figsize=(8,4))
plt.plot(x, y)
ticks = np.arange(x[0], x[-1], (x[-1] - x[0]).astype('timedelta64[D]') / 15)
plt.xticks(ticks, ticks)
plt.gca().tick_params('x', labelrotation=90, labelsize=10)
plt.show()
This will give you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.