减少Matplotlib中面积图中的x轴条目 [英] Reduce x-axis entries in an area plot in Matplotlib

查看:32
本文介绍了减少Matplotlib中面积图中的x轴条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要具有重叠曲线的非堆叠面积图,这些曲线在某种意义上是透明的,您可以看到两条曲线(类似于

解决方案

要设置自定义标签,您可以采用玩具示例中显示的以下方法:

将 numpy 导入为 np导入matplotlib.pyplot作为plt导入matplotlib.ticker作为股票行情x = [0,5,9,10,15,22,55,88,122]y = x无花果,ax = plt.subplots()ax.plot(x,y)xticks = ['00:00','01:00','02:00','24:00']xvals = [0,22,88,122]ax.set(xticks = xvals,xticklabels = xticks)plt.show()

在您的特定情况下,您每 5 分钟就有一次值.您希望每 60 分钟进行一次滴答.

  xticks = ['00:00','01:00','02:00','03:00','04:00','05:00','06:00','07:00','08:00','09:00','10:00','11:00','12:00','13:00','14:00','15:00','16:00','17:00','18:00','19:00','20:00','21:00','22:00','23:00']xvals = power_values [:: 12]#每12个元素对应1小时.

I want to have not-stacked area plot with overalapping curves that are transparent in a sense that you can see both curves (similar to Custom legend for overlapping transparent areas with exact colors in a python pandas dataframe for stacked=false?). The problem is that I have many entries and because of this the current plot looks quite bad (see screenshot). For every 5 minute of a day I have an entry which makes 288 entries for a whole day. I would like to only have x-axis only for the hours of the day like: 00:00, 01:00, 02:00 ... 24:00. How can I do that?

Here is my current code:

    import pandas as pd
from matplotlib import pyplot as plt
%matplotlib inline

columns = ['Method 1', 'Method 2']
power_values = [[120000, 12000]
,[120000, 116321.1908]
,[120000, 118113.0328]
,[120000, 114600]
,[93000, 107353.9204]
,[93000, 107587.8386]
,[12000, 117104.2044]
,[12000, 118264.4594]
,[12000, 117945.2063]
,[12000, 119603.5571]
,[12000, 114533.1649]
,[12000, 115572.16]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12838.06104]
,[12000, 12083.09783]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 12000]
,[12000, 89786.61005]
,[12000, 77881.9384]
,[12000, 92682.10154]
,[12000, 87809.53293]
,[12000, 86967.55995]
,[12000, 66165.492]
,[12000, 70745.2192]
,[14700, 65919.1703]
,[14700, 60105.36791]
,[14700, 71163.80286]
,[14700, 41359.14048]
,[17400, 86868.4165]
,[17400, 17369.06999]
,[22800, 24852.58464]
,[28200, 32362.0731]
,[30900, 26377.3125]
,[39000, 31773.85124]
,[39000, 43314.7621]
,[39000, 48467.08134]
,[39000, 61814.72004]
,[39000, 47418.45997]
,[41700, 58048.10651]
,[47100, 50910.68421]
,[47100, 88949.26413]
,[47100, 46397.97818]
,[55200, 54805.75834]
,[57900, 40667.24158]
,[63300, 72125.98178]
,[66000, 62627.80501]
,[66000, 60919.18474]
,[66000, 59630.61079]
,[74100, 67263.35234]
,[74100, 56496.24679]
,[79500, 69745.10363]
,[82200, 87378.8584]
,[84900, 95942.32954]
,[87600, 12000]
,[87600, 12176.49049]
,[95700, 48300.76603]
,[95700, 83070.30318]
,[95700, 100654.9642]
,[103800, 105354.238]
,[106500, 110412.9039]
,[109200, 105819.7575]
,[106500, 92972.19054]
,[106500, 94560.84184]
,[106500, 120000]
,[111900, 117985.9067]
,[109200, 12000]
,[111900, 12000]
,[114600, 12000]
,[106500, 12000]
,[101100, 12000]
,[98400, 12000]
,[93000, 12000]
,[93000, 13746.58733]
,[93000, 12000]
,[93000, 12000]
,[93000, 12000]
,[93000, 12000]
,[87600, 12000]
,[87600, 12000]
,[84900, 12000]
,[82200, 12000]
,[76800, 12000]
,[71400, 12000]
,[68700, 12000]
,[68700, 12000]
,[68700, 12000]
,[66000, 12000]
,[63300, 12000]
,[60600, 12000]
,[60600, 42243.72896]
,[57900, 54527.61265]
,[52500, 70358.99518]
,[49800, 78872.43535]
,[47100, 94230.28391]
,[49800, 105183.383]
,[52500, 111166.1544]
,[49800, 111937.2309]
,[49800, 115286.2033]
,[49800, 117300]
,[49800, 119554.2029]
,[44400, 119916.9913]
,[47100, 120000]
,[44400, 120000]
,[44400, 120000]
,[44400, 120000]
,[44400, 120000]
,[47100, 120000]
,[49800, 120000]
,[49800, 120000]
,[47100, 120000]
,[47100, 120000]
,[47100, 120000]
,[55200, 120000]
,[57900, 12000]
,[57900, 12000]
,[55200, 12000]
,[55200, 12000]
,[55200, 12000]
,[60600, 12000]
,[55200, 12000]
,[49800, 12000]
,[52500, 12000]
,[55200, 15962.01285]
,[52500, 23219.24871]
,[60600, 33698.20201]
,[60600, 119930.2521]
,[63300, 120000]
,[60600, 119709.8892]
,[63300, 119621.9221]
,[66000, 120000]
,[66000, 117300]
,[71400, 120000]
,[68700, 120000]
,[71400, 119836.1707]
,[60600, 117300]
,[57900, 119537.0779]
,[60600, 120000]
,[63300, 12000]
,[60600, 12000]
,[55200, 12000]
,[57900, 12000]
,[55200, 12000]
,[52500, 18663.70438]
,[47100, 28437.57919]
,[41700, 70171.49386]
,[39000, 100169.5002]
,[39000, 114429.5489]
,[39000, 112166.1558]
,[39000, 120000]
,[36300, 12000]
,[30900, 12000]
,[30900, 12000]
,[28200, 12000]
,[30900, 12000]
,[30900, 12000]
,[28200, 12000]
,[25500, 12835.81588]
,[28200, 35718.64701]
,[33600, 97075.89193]
,[36300, 111813.347]
,[39000, 117276.4379]
,[41700, 12000]
,[44400, 12000]
,[44400, 12000]
,[44400, 12000]
,[47100, 12000]
,[44400, 12000]
,[44400, 12000]
,[47100, 12000]
,[44400, 12000]
,[47100, 12000]
,[47100, 12000]
,[49800, 12000]
,[47100, 12000]
,[57900, 12000]
,[57900, 12000]
,[57900, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12000]
,[66000, 12000]
,[63300, 12000]
,[68700, 12000]
,[68700, 12000]
,[74100, 12000]
,[79500, 12000]
,[79500, 12000]
,[84900, 12000]
,[84900, 12000]
,[82200, 12000]
,[82200, 12000]
,[82200, 12000]
,[76800, 12000]
,[79500, 12000]
,[82200, 12000]
,[82200, 12000]
,[84900, 12000]
,[87600, 12000]
,[87600, 12000]
,[79500, 12000]
,[71400, 12000]
,[71400, 12000]
,[71400, 12000]
,[71400, 12000]
,[63300, 12000]
,[66000, 12000]
,[63300, 12000]
,[60600, 12000]
,[60600, 12000]
,[63300, 12000]
,[63300, 12000]
,[63300, 12154.35357]
,[66000, 12536.75926]
,[66000, 12903.98697]
,[55200, 13053.31214]
,[55200, 13542.66462]
,[55200, 13650.02032]
,[52500, 14112.16008]
,[41700, 14044.3751]
,[41700, 14061.56448]
,[44400, 50555.92643]
,[41700, 67285.26901]
,[41700, 29504.2061]
,[39000, 23825.30113]
,[33600, 29774.46277]
,[33600, 79602.37682]
,[30900, 41151.24172]
,[30900, 65128.04827]
,[30900, 19498.52188]
,[33600, 30631.10012]
,[33600, 18007.39888]
,[36300, 12000]
,[33600, 114692.7977]
,[30900, 49522.07339]
,[33600, 93953.58247]
,[36300, 74269.44322]
,[36300, 52006.01276]
,[30900, 12000]
,[36300, 12000]
,[36300, 12000]
,[36300, 12000]
,[33600, 12000]
,[33600, 12000]
,[36300, 12000]
,[36300, 85105.38741]
,[36300, 30555.82867]
,[36300, 66040.11056]
,[39000, 45224.85527]
,[36300, 28136.72986]
,[36300, 12186.79167]
,[33600, 12000]
,[33600, 12000]
,[30900, 12000]
,[25500, 12000]
,[25500, 12000]
,[25500, 12000]
,[28200, 76832.66527]
,[28200, 43609.02258]
,[28200, 67022.7672]
,[25500, 73925.53359]
,[25500, 53049.44227]
,[20100, 53800.767]
,[20100, 48240.78759]
,[20100, 38755.82412]
,[20100, 55760.30678]
,[20100, 60444.88024]
,[20100, 59739.8675]
,[20100, 47365.30315]
,[17400, 47365.30315]]

wind_data = pd.DataFrame(power_values, index=range(0, 289), columns=columns)
fig = plt.figure(linewidth=1, figsize=(7, 5))
ax = wind_data.plot.area(ax=plt.gca(), color=plt.get_cmap('Set1').colors, stacked=False)
ax.set_facecolor("white")
ax.set_xlabel("Time of day", fontsize = 14, labelpad=8)
ax.set_ylabel("Power in kW", fontsize = 14,labelpad=8)
ax.set_xlim(0, 288)
ax.set_ylim(0, 300000)
plt.xticks(wind_data.index, labels=[f'{h:02d}:00' for h in wind_data.index], rotation=90)
plt.grid(axis='y', alpha=.4)
plt.tight_layout()
ax.tick_params(axis='both', which='major', labelsize=14)
ax.legend(loc='center left', bbox_to_anchor=(0.03, 1.15), fontsize = 14, ncol=3)
plt.savefig('Wind_Assignment_NoScore.png', edgecolor='black', dpi=300, bbox_inches='tight')
plt.show()

Here is the current figure:

解决方案

To set custom labels, you can take the following approach shown on a toy example:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker

x = [0,5,9,10,15,22,55,88,122]
y = x
fig, ax = plt.subplots()
ax.plot(x,y)
xticks = ['00:00', '01:00', '02:00' , '24:00']
xvals = [0,22,88,122] 
ax.set(xticks=xvals, xticklabels=xticks)
plt.show()

In your particular case, you've values every 5 minutes. You want to have ticks every 60 minutes.

xticks=['00:00','01:00','02:00','03:00','04:00','05:00',
      '06:00','07:00','08:00','09:00','10:00','11:00',
      '12:00','13:00','14:00','15:00','16:00','17:00',
      '18:00','19:00','20:00','21:00','22:00','23:00']

xvals=power_values[::12] # every 12th element corresponds to 1hr.

这篇关于减少Matplotlib中面积图中的x轴条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆