Matplotlib x-axis ограниченный диапазон - PullRequest
1 голос
/ 12 марта 2020

Итак, я пытался представить некоторые данные. Ось X ограничена двумя годами. Мой вопрос довольно прост, может кто-нибудь объяснить, почему ось X ограничена диапазоном дат с 2015Q1 по 2017Q1, когда доступные данные находятся между 2015Q1 - 2020Q1. Что-то отсутствует или неверно в моем коде?

dd2

qtr     median  count
0   2015Q1  1290000.0   27
1   2015Q2  1330000.0   43
2   2015Q3  1570000.0   21
3   2015Q4  1371000.0   20
4   2016Q1  1386500.0   20
5   2016Q2  1767500.0   22
6   2016Q3  1427500.0   32
7   2016Q4  1501000.0   31
8   2017Q1  1700000.0   29
9   2017Q2  1630000.0   15
10  2017Q3  1687500.0   24
11  2017Q4  1450000.0   15
12  2018Q1  1505000.0   13
13  2018Q2  1494000.0   14
14  2018Q3  1415000.0   21
15  2018Q4  1150000.0   15
16  2019Q1  1228000.0   15
17  2019Q2  1352500.0   12
18  2019Q3  1237500.0   12
19  2019Q4  1455000.0   26
20  2020Q1  1468000.0   9

код

x = dd2['qtr']


y1 = dd2['count']
y2 = dd2['median']



fig, ax = plt.subplots(figsize=(40,10))

ax = plt.subplot(111)
ax2 = ax.twinx()


y1_plot = y1.plot(ax=ax2, color='green', legend=True, marker='*', label="median")
y2_plot = y2.plot(ax=ax, color='red',   legend=True, linestyle='--', marker='x', label="count")


plt.title('Price trend analysis')
ax.set_xticklabels(x, rotation='vertical',color='k', size=20)

ax.set_xlabel('year')
ax.set_ylabel('sold price')
ax2.set_ylabel('number of sales')

y1_patch = mpatches.Patch(color='red', label='median sold price')
y2_patch = mpatches.Patch(color='green', label='count')
plt.legend(handles=[y2_patch,y1_patch],loc='upper right')


plt.savefig('chart.png', dpi=300,bbox_inches ='tight')
plt.show()

enter image description here

Ответы [ 2 ]

0 голосов
/ 13 марта 2020

Вместо того, чтобы использовать Pandas 'методы построения серии, я бы использовал pyplot, чтобы построить ваши x и y данные вместе, как здесь:

# everything is the same up to 'ax2 = ax.twinx()'

# plot on your axes, save a reference to the line
line1 = ax.plot(x, y1, color="green", label="median sold price", marker='*')
line2 = ax2.plot(x, y2, color="red", label="count", marker='x')

# no need for messing with patches
lines = line1 + line2
labels = [l.get_label() for l in lines]
ax.legend(lines, labels, loc='upper right')

# this is the same as before again
plt.title('Price trend analysis')
ax.xaxis.set_tick_params(rotation=90, color='k', size
ax.set_xlabel('year')
ax.set_ylabel('sold price')
ax2.set_ylabel('number of sales')

plt.savefig('chart.png', dpi=300,bbox_inches ='tight')
plt.show()
0 голосов
/ 12 марта 2020

используя mtick для построения всех данных по оси X.

import matplotlib.ticker as mtick

ax.xaxis.set_major_locator(mtick.IndexLocator(base=1, offset=0))

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...