0

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!

1 Answer 1

1

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:

enter image description here

2
  • Thank you for your helpful answer! Is it somehow possible to set the color/marker based on the name of the column?
    – PDistl
    Feb 18, 2021 at 13:19
  • You mean your columns are something like "A1, A2, B1, B2, B3, B4"? I assumed from "x+1 to B in another color with the same markers" that the groups would be of equal length ("same markers"). If that is not the case, how do you define the groups for color A and color B?
    – Mr. T
    Feb 18, 2021 at 13:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.