简体   繁体   中英

Plotting pandas dataframe with one color for several columns

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:

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM