擅长:python、mysql、java
<p>这是一种方法,使用<code>groupby</code>基于<code>Town</code>形成组,然后使用次y轴绘制<code>price</code>和<code>sales</code></p>
<pre><code>fig, ax = plt.subplots(figsize=(8, 6))
df_group = data.groupby('Town')['week','price','sales']
ylabels = ['price', 'sales']
colors =['r', 'g', 'b']
for i, key in enumerate(df_group.groups.keys()):
df_group.get_group(key).plot('week', 'price', color=colors[i], ax=ax, label=key)
df_group.get_group(key).plot('week', 'sales', color=colors[i], linestyle=' ', secondary_y=True, ax=ax)
handles,labels = ax.get_legend_handles_labels()
legends = ax.legend()
legends.remove()
plt.legend(handles, labels)
ax1.set_ylabel('Price')
ax2.set_ylabel('Sales')
</code></pre>
<p><a href="https://i.stack.imgur.com/DD7cY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DD7cY.png" alt="enter image description here"/></a></p>