如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?

时间:2022-02-08 08:13:01

Suppose I have the following two dataframes:

假设我有以下两个数据帧:

df1 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()
df2 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()

如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?

如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?

My question is that, how can I plot them in one graph such that:

我的问题是,如何在一个图中绘制它们,以便:

  1. The three series of df1 and df2 are still in the same blue, orange and green lines as above.
  2. 三个系列的df1和df2仍然是如上所述的蓝色,橙色和绿色线。
  3. The three series of df1 are in solid lines
  4. df1的三个系列是实线
  5. The three series of df1 are in dashed lines
  6. df1的三个系列是虚线

Currently the closest thing I can get is the following:

目前我能得到的最接近的是:

ax = df1.plot(style=['b','y','g'])
df2.plot(ax=ax, style=['b','y','g'], linestyle='--')

如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?

Is there any way to get the color codes used by default by DataFrame.plot()? Or is there any other better approach to achieve what I want? Ideally I don't want to specify any color codes with the style parameter but always use the default colors.

有没有办法获取DataFrame.plot()默认使用的颜色代码?或者还有其他更好的方法来实现我想要的吗?理想情况下,我不想使用style参数指定任何颜色代码,但始终使用默认颜色。

3 个解决方案

#1


3  

Without messing with the colors themselves or transferring them from one plot to the other you may easily just reset the colorcycle in between your plot commands

在不弄乱颜色本身或将它们从一个绘图转移到另一个绘图的情况下,您可以轻松地在绘图命令之间重置颜色循环

ax = df1.plot()
ax.set_prop_cycle(None)
df2.plot(ax=ax, linestyle="--")

#2


5  

You can get the default color parameters that are currently being used from matplotlib.

您可以从matplotlib获取当前正在使用的默认颜色参数。

import matplotlib.pyplot as plt
colors = list(plt.rcParams.get('axes.prop_cycle'))

[{'color': '#1f77b4'},
 {'color': '#ff7f0e'},
 {'color': '#2ca02c'},
 {'color': '#d62728'},
 {'color': '#9467bd'},
 {'color': '#8c564b'},
 {'color': '#e377c2'},
 {'color': '#7f7f7f'},
 {'color': '#bcbd22'},
 {'color': '#17becf'}]

so just pass style=['#1f77b4', '#ff7f0e', '#2ca02c'] and the colors should work.

所以只需传递style = ['#1f77b4','#ff7f0e','#2ca02c'],颜色应该有效。

If you want to set another color cycler, say the older version, then:

如果你想设置另一个颜色循环器,比如旧版本,那么:

plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'bgrcmyk')")
list(plt.rcParams['axes.prop_cycle'])
#[{'color': 'b'},
# {'color': 'g'},
# {'color': 'r'},
# {'color': 'c'},
# {'color': 'm'},
# {'color': 'y'},
# {'color': 'k'}]

#3


4  

You could use get_color from the lines:

您可以使用以下行中的get_color:

df1 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()
df2 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()

ax = df1.plot()
l = ax.get_lines()
df2.plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))

Output:

输出:

如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?

#1


3  

Without messing with the colors themselves or transferring them from one plot to the other you may easily just reset the colorcycle in between your plot commands

在不弄乱颜色本身或将它们从一个绘图转移到另一个绘图的情况下,您可以轻松地在绘图命令之间重置颜色循环

ax = df1.plot()
ax.set_prop_cycle(None)
df2.plot(ax=ax, linestyle="--")

#2


5  

You can get the default color parameters that are currently being used from matplotlib.

您可以从matplotlib获取当前正在使用的默认颜色参数。

import matplotlib.pyplot as plt
colors = list(plt.rcParams.get('axes.prop_cycle'))

[{'color': '#1f77b4'},
 {'color': '#ff7f0e'},
 {'color': '#2ca02c'},
 {'color': '#d62728'},
 {'color': '#9467bd'},
 {'color': '#8c564b'},
 {'color': '#e377c2'},
 {'color': '#7f7f7f'},
 {'color': '#bcbd22'},
 {'color': '#17becf'}]

so just pass style=['#1f77b4', '#ff7f0e', '#2ca02c'] and the colors should work.

所以只需传递style = ['#1f77b4','#ff7f0e','#2ca02c'],颜色应该有效。

If you want to set another color cycler, say the older version, then:

如果你想设置另一个颜色循环器,比如旧版本,那么:

plt.rcParams['axes.prop_cycle'] = ("cycler('color', 'bgrcmyk')")
list(plt.rcParams['axes.prop_cycle'])
#[{'color': 'b'},
# {'color': 'g'},
# {'color': 'r'},
# {'color': 'c'},
# {'color': 'm'},
# {'color': 'y'},
# {'color': 'k'}]

#3


4  

You could use get_color from the lines:

您可以使用以下行中的get_color:

df1 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()
df2 = pd.DataFrame(np.random.randn(100, 3),columns=['A','B','C']).cumsum()

ax = df1.plot()
l = ax.get_lines()
df2.plot(ax=ax, linestyle='--', color=(i.get_color() for i in l))

Output:

输出:

如何在一个具有相同颜色但不同线条样式的图形中绘制两个pandas DataFrame?