用一种颜色为多列绘制 pandas 数据框 [英] Plotting pandas dataframe with one color for several columns

查看:38
本文介绍了用一种颜色为多列绘制 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的数据框:

  A ... B约会时间                            ...2020-01-01 00:00:00 10.622 ... 302020-01-01 01:00:00 16.397 ... 302020-01-01 02:00:00 24.190 ... 302020-01-01 03:00:00 33.579 ... 302020-01-01 04:00:00 44.643 ... 30…………2020-01-07 20:00:00 18.090 ... 302020-01-07 21:00:00 18.027 ... 30

当我使用 df.plot 时,所有列都使用默认颜色和实线绘制.我想用相同的颜色但标记不同来绘制A到x列,而用相同的标记另一种颜色来绘制x + 1到B列.

如果不为每一列分别手动声明,这是否可行?

谢谢!

解决方案

您可以创建

I have a dataframe that looks like this:

                                 A  ...            B
datetime                            ...             
2020-01-01 00:00:00         10.622  ...           30
2020-01-01 01:00:00         16.397  ...           30
2020-01-01 02:00:00         24.190  ...           30
2020-01-01 03:00:00         33.579  ...           30
2020-01-01 04:00:00         44.643  ...           30
                            ...     ...           ...
2020-01-07 20:00:00         18.090  ...           30
2020-01-07 21:00:00         18.027  ...           30

When I use df.plot, all columns are plotted with default colors and solid lines. I would like to plot columns A to x in the same color but with different markers and columns x+1 to B in another color with the same markers.

Is this somehow possible without manually declaring it for every column seperately?

Thanks!

解决方案

You can create a custom cycler for this task:

from cycler import cycler
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

markernr = 3
colornr = 2
#test data generation
np.random.seed(123)
df = pd.DataFrame(np.random.random((10, 6)), columns=["A1", "A2", "A3", "B1", "B2", "B3"])

colors = ["tab:red", "tab:blue", "tab:orange", "brown", "lightblue", "yellow"]
markers = ["x", "o", "D", "+", "H"]

my_cycler = (cycler(color=colors[:colornr]) *
             cycler(marker=markers[:markernr]))

fig, (ax1, ax2) = plt.subplots(1, 2, figsize=(10, 6))

df.plot(ax=ax1, title="Standard cycler")

ax2.set_prop_cycle(my_cycler)
df.plot(ax=ax2, title="Customized cycler")

plt.show()

Sample output:

这篇关于用一种颜色为多列绘制 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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