在python中复制R/ggplot2颜色

Mar*_*aph 3 python matplotlib python-ggplot

此链接具有R代码以复制ggplot的颜色:使用qplot绘制函数系列而不复制数据

我已经去过复制python中的代码 - 但结果不对...

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import math
import colorsys

# function to return a list of hex colour strings
def colorMaker(n=12, start=15.0/360.0, saturation=1.0, valight=0.65) :
    listOfColours = []
    for i in range(n) :
        hue = math.modf(float(i)/float(n) + start)[0]
        #(r,g,b) = colorsys.hsv_to_rgb(hue, saturation, valight)
        (r,g,b) = colorsys.hls_to_rgb(hue, valight, saturation)
        listOfColours.append( '#%02x%02x%02x' % (int(r*255), int(g*255), int(b*255)) )
    return listOfColours

# made up data
x = np.array(range(20))
d = {}
d['y1'] = pd.Series(x, index=x)
d['y2'] = pd.Series(1.5*x + 1, index=x)
d['y3'] = pd.Series(2*x + 2, index=x)
df = pd.DataFrame(d)

# plot example
plt.figure(num=1, figsize=(10,5), dpi=100) # set default image size
colours = colorMaker(n=3)
df.plot(linewidth=2.0, color=colours)
fig = plt.gcf()
fig.savefig('test.png')
Run Code Online (Sandbox Code Playgroud)

结果 ...

在此输入图像描述

Jak*_*kob 8

你应该检查mpltools一个非常简洁的matplotlib扩展.有一个ggplot风格看这里.例如(来自链接): 在此输入图像描述

  • +1.我还想建议Seaborn:https://github.com/mwaskom/seaborn; Python-ggplot:https://github.com/yhat/ggplot; 和prettyplotlib:https://github.com/olgabot/prettyplotlib (4认同)