使用Matplotlib格式化绘图

2024-06-16 10:14:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我是熊猫和Matplotlib的初学者,在使用熊猫时,我对格式化绘图有一些问题。对于面积图,我有以下代码:

import pandas as pd
from matplotlib import pyplot as plt

ax= wind_data.plot.area()
plt.legend(loc='center left', bbox_to_anchor= (1.0, 0.5))
plt.grid(False)
ax.set_facecolor("white")
ax.set_xlabel("Time of day")
ax.set_ylabel("Power in kW")
ax.set_xlim(0,24)
ax.set_ylim(0,50)

现在我想改变以下几个方面:

  • x轴上的值是一天中的时间。它们应该从00:00-24:00开始显示。所以每小时都应该有一个条目。由于空间不足,条目应垂直(而不是水平)书写
  • 当我导出png文件时,绘图周围有一个灰色框。不应该是这样。相反,应该有一条细黑线作为边缘
  • 我想在y轴的每个入口都画一条水平线。这条线不应该太强,而是以某种方式透明,这样你就可以看到它,而不是占主导地位。你知道吗

是否可以使用Matplotlib(或任何其他python库)执行此操作?你知道吗

编辑:这里有输入数据:

Building 1  Building 2  Building 3  Building 4  Building 5
7.04    7.04    7.04    7.04    7.04
6.36    6.36    6.36    6.36    6.36
6.4     6.4     6.4     6.4     6.4
6.1     6.1     6.1     6.1     6.1
5.88    5.88    5.88    5.88    5.88
6.18    6.18    6.18    6.18    6.18
6.16    6.16    6.16    6.16    6.16
5.82    5.82    5.82    5.82    5.82
5.28    5.28    5.28    5.28    5.28
4.82    4.82    4.82    4.82    4.82
4.18    4.18    4.18    4.18    4.18
4.02    4.02    4.02    4.02    4.02
4.08    4.08    4.08    4.08    4.08
4.24    4.24    4.24    4.24    4.24
6.24    6.24    6.24    6.24    6.24
8.44    8.44    8.44    8.44    8.44
8.72    8.72    8.72    8.72    8.72
8.06    8.06    8.06    8.06    8.06
7.16    7.16    7.16    7.16    7.16
6.52    6.52    6.52    6.52    6.52
7.16    7.16    7.16    7.16    7.16
7.88    7.88    7.88    7.88    7.88
8.44    8.44    8.44    8.44    8.44
8.56    8.56    8.56    8.56    8.56

编辑:在Jupyter中使用JohanC的解决方案代码时出现错误消息

AttributeError                            Traceback (most recent call last)
<ipython-input-5-51251d64e3e0> in <module>()
     21 ax.set_xlim(1, 24)
     22 ax.set_ylim(0, 50)
---> 23 plt.xticks(wind_data.index, labels=[f'{h:02d}:00' for h in wind_data.index], rotation=90)
     24 plt.grid(axis='y', alpha=.4)
     25 plt.tight_layout()

C:\Users\wi9632\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\pyplot.py in xticks(*args, **kwargs)
   1704     if len(kwargs):
   1705         for l in labels:
-> 1706             l.update(kwargs)
   1707 
   1708     return locs, silent_list('Text xticklabel', labels)

C:\Users\wi9632\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\text.py in update(self, kwargs)
    241         """
    242         bbox = kwargs.pop('bbox', None)
--> 243         super(Text, self).update(kwargs)
    244         if bbox:
    245             self.set_bbox(bbox)  # depends on font properties

C:\Users\wi9632\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py in update(self, props)
    883         try:
    884             ret = [_update_property(self, k, v)
--> 885                    for k, v in props.items()]
    886         finally:
    887             self.eventson = store

C:\Users\wi9632\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py in <listcomp>(.0)
    883         try:
    884             ret = [_update_property(self, k, v)
--> 885                    for k, v in props.items()]
    886         finally:
    887             self.eventson = store

C:\Users\wi9632\AppData\Local\Continuum\Anaconda3\lib\site-packages\matplotlib\artist.py in _update_property(self, k, v)
    876                 func = getattr(self, 'set_' + k, None)
    877                 if func is None or not six.callable(func):
--> 878                     raise AttributeError('Unknown property %s' % k)
    879                 return func(v)
    880 

AttributeError: Unknown property labels

Tags: inselfmatplotliblocalupdatepltaxusers
1条回答
网友
1楼 · 发布于 2024-06-16 10:14:53

下面是一个使用matplotlib的示例。现在是更新问题的数据。你知道吗

对于xticks,可以使用plt.xticks公司()``,也接受旋转角度。要获得黑色边框,似乎需要通过plt.figure设置linewidth,而edgecolor是plt.savefig的参数。你知道吗

plt.grid有参数显示一个或两个方向的网格线。可以设置alpha值,使这些行不那么突出或更加突出。你知道吗

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

columns = ['Building 1', 'Building 2', 'Building 3', 'Building 4', 'Building 5']
power_values = [[7.04, 7.04, 7.04, 7.04, 7.04], [6.36, 6.36, 6.36, 6.36, 6.36], [6.4, 6.4, 6.4, 6.4, 6.4],
                [6.1, 6.1, 6.1, 6.1, 6.1], [5.88, 5.88, 5.88, 5.88, 5.88], [6.18, 6.18, 6.18, 6.18, 6.18],
                [6.16, 6.16, 6.16, 6.16, 6.16], [5.82, 5.82, 5.82, 5.82, 5.82], [5.28, 5.28, 5.28, 5.28, 5.28],
                [4.82, 4.82, 4.82, 4.82, 4.82], [4.18, 4.18, 4.18, 4.18, 4.18], [4.02, 4.02, 4.02, 4.02, 4.02],
                [4.08, 4.08, 4.08, 4.08, 4.08], [4.24, 4.24, 4.24, 4.24, 4.24], [6.24, 6.24, 6.24, 6.24, 6.24],
                [8.44, 8.44, 8.44, 8.44, 8.44], [8.72, 8.72, 8.72, 8.72, 8.72], [8.06, 8.06, 8.06, 8.06, 8.06],
                [7.16, 7.16, 7.16, 7.16, 7.16], [6.52, 6.52, 6.52, 6.52, 6.52], [7.16, 7.16, 7.16, 7.16, 7.16],
                [7.88, 7.88, 7.88, 7.88, 7.88], [8.44, 8.44, 8.44, 8.44, 8.44], [8.56, 8.56, 8.56, 8.56, 8.56]]
wind_data = pd.DataFrame(power_values, index=range(1, 25), columns=columns)
fig = plt.figure(linewidth=1, figsize=(7, 5))
ax = wind_data.plot.area(ax=plt.gca(), color=plt.get_cmap('Set1').colors)
plt.legend(loc='center left', bbox_to_anchor=(1.0, 0.5))
ax.set_facecolor("white")
ax.set_xlabel("Time of day")
ax.set_ylabel("Power in kW")
ax.set_xlim(1, 24)
ax.set_ylim(0, 50)
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()
plt.savefig('wind_data.png', edgecolor='black', dpi=300)
plt.show()

example plot

PS:如果您的Python版本早于3.6,请将f'{h:02d}:00'替换为'%02d:00' % h。你知道吗

相关问题 更多 >