轻松设置自动应用于 matplotlib、seaborn 和 pandas 绘图的主题
项目描述
pyplot 主题
您可以看到适用于 matplotlib、seaborn 和 pandas 绘图的主题。
这个包的灵感来自ggthemes于 R 中的包,以及受seabornpython中的包影响的代码(特别是rcmod.py)。
安装
从 PyPI 安装
pip install pyplot-themes
或者直接从 GitHub
pip install git+https://github.com/raybuhr/pyplot-themes.git
用法
环境
import sys
sys.version
'3.7.1 (default, Dec 14 2018, 19:28:38) \n[GCC 7.3.0]'
import matplotlib.pyplot as plt
from seaborn import palplot # only used to show off palettes
from string import ascii_uppercase
import numpy as np
def example_scatter_plot(num_cats=6):
for i in range(num_cats):
cat = ascii_uppercase[i]
x = np.random.random(100)
y = np.random.random(100) + i
plt.scatter(x, y, marker='o', label=cat)
plt.legend(loc='best')
def example_bar_plot(num_cats=6):
bar_width = 1 / num_cats + 1
for i in range(num_cats):
cat = ascii_uppercase[i]
x = np.arange(11) + 5 * i
y = np.array([0, 1, 2, 3, 4, 5, 4, 3, 2, 1, 0]) + np.random.random(1)
plt.bar(x, y, label=cat, width=bar_width)
plt.legend(loc='best')
def example_plots(num_cats=6):
example_scatter_plot(num_cats)
plt.show()
example_bar_plot(num_cats)
plt.show()
默认 Maplotlib 主题
example_plots()
如您所见,默认主题在颜色上有很好的对比,但在图表的大小(即图形大小又名 figsize)和字体方面还有一些不足之处。
用法
import pyplot_themes as themes
themes.__version__
'0.2.0'
themes.theme_minimal()
这使用色盲安全颜色更新了 matplotlib 的全局主题设置,具有极简风格。
palplot(themes.palettes.Colorblind.colors)
example_plots()
如您所见,我们的绘图现在要大得多,具有可访问的颜色,并且有一些浅色网格线可以更容易地识别值。
所有主题都有一些参数可用:
- 网格:打开/关闭网格线
- ticks:打开/关闭刻度线
- figsize:设置绘图的默认大小(如果需要,您仍然可以以临时方式更改每个绘图)
- fontsize:设置要使用的默认字体大小
有些主题允许你传入任何你想要的颜色,有些主题你必须从可用选项中选择一个配色方案,有些主题只允许你颠倒默认调色板的顺序,有些不允许你弄乱颜色全部。尝试并找出你喜欢什么。
themes.theme_minimal(grid=False, ticks=False, fontsize=18)
example_scatter_plot()
plt.title("Look Mom, no lines!")
Text(0.5, 1.0, 'Look Mom, no lines!')
主题
主题。theme_dark ()
example_plots()
主题。theme_tableau ()
example_plots()
palplot(themes.palettes.Solarized.dark)
主题。theme_solarized ( scheme = "dark" )
example_plots()
palplot(themes.palettes.Solarized.light)
主题。theme_solarized ( scheme = "light" )
example_plots()
palplot(themes.palettes.PaulTolColorSchemes.colors)
themes.theme_paul_tol()
example_plots(12)
themes.theme_paul_tol(reverse_colors=True, grid=False)
example_plots(num_cats=12)
palplot(themes.palettes.Few.light)
palplot(themes.palettes.Few.medium)
palplot(themes.palettes.Few.dark)
主题。theme_few ( scheme = "light" )
example_plots()
themes.theme_few(scheme="medium", figsize=[5, 5])
example_scatter_plot()
themes.theme_few(scheme="dark")
example_bar_plot()
themes.theme_ucberkeley(figsize=[10, 5])
example_plots(num_cats=4)
themes.theme_ucberkeley(scheme="all", figsize=[12, 6])
example_plots(num_cats=16)
matplotlib 附带的主题
这些下一个主题实际上是 matplotlib 附带的,您可以在没有pyplot-themes包的情况下使用它们。这里的函数基本上是用于调用 matplotlib 定义的样式的瘦包装器,但默认使用更大的 figsize。
主题。theme_fivethirtyight ()
example_plots()
主题。theme_ggplot2 ( figsize = [ 10 , 5 ])
example_plots()
bmh代表黑客的贝叶斯方法
themes.theme_bmh()
example_scatter_plot()
所以我们也有一个拼写版本的别名,以便更容易发现
themes.theme_bayesian_methods_for_hackers()
example_bar_plot()
虽然这个包提供了浅色和深色的日光主题,但matplotlib也有一个浅色版本。如果您想保持绘图颜色的更多对比度,这是一个不错的选择。
主题。theme_solarized_light2 ()
example_plots()
修改主题
除了便于查找和调用 matplotlib 主题外,pyplot-themes还可以更轻松地对其进行轻微修改。例如,假设您想使用ggplot2主题,但您想使用 Paul Tol 颜色方案调色板。
themes.theme_ggplot2(palette=themes.palettes.PaulTolColorSchemes.colors, figsize=[12, 6])
example_bar_plot(num_cats=12)
或者也许是fivethirtyeight颜色
themes.theme_ggplot2(palette=themes.palettes.FiveThirtyEight.colors)
example_bar_plot()
重置为 matplotlib 默认值
当然,有时当您尝试不同的主题时,您可能会发现您修改了一个您不太喜欢的设置,但不确定发生了什么变化。为了帮助调试,我们创建了一个函数来将主题重置为 matplotlib 的开头。当然,您可能只是喜欢 matplotlib 默认值,这没关系。
注意:matplotlib 的默认设置可能会略有不同,具体取决于您是在 python 文件(例如脚本)中使用还是在 jupyter 笔记本中使用%matplotlib inline. 重置功能假定您默认使用笔记本,但如果您不是,则提供一个参数来关闭它:
themes.theme_reset(notebook=False)
themes.theme_reset() # could also use the alias `themes.theme_matplotlib_default()`
example_bar_plot()
调色板
除了上述主题外,还提供了一堆调色板。这里有一些要炫耀的。
palplot(themes.palettes.Autumn1.colors)
palplot(themes.palettes.Autumn2.colors)
palplot(themes.palettes.Canyon.colors)
palplot(themes.palettes.Chili.colors)
palplot(themes.palettes.Tomato.colors)
palplot(themes.palettes.Few.medium)
palplot(themes.palettes.FiveThirtyEight.colors)
palplot(themes.palettes.Solarized.light)
palplot(themes.palettes.Solarized.dark)
palplot(themes.palettes.UCBerkeley.primary_colors)
palplot(themes.palettes.UCBerkeley.secondary_colors)
顺序调色板
palplot(themes.palettes.Sequential.blues)
palplot(themes.palettes.Sequential.cyans)
palplot(themes.palettes.Sequential.purples)
palplot(themes.palettes.Sequential.greens)
palplot(themes.palettes.Sequential.oranges)
palplot(themes.palettes.Sequential.reds)
发散的调色板
palplot(themes.palettes.Diverging.blueorange)
palplot(themes.palettes.Diverging.orangeblue)
palplot(themes.palettes.Diverging.bluepurple)
palplot(themes.palettes.Diverging.purpleblue)
palplot(themes.palettes.Diverging.bluered)
palplot(themes.palettes.Diverging.redblue)
palplot(themes.palettes.Diverging.greenpurple)
palplot(themes.palettes.Diverging.purplegreen)
palplot(themes.palettes.Diverging.greenred)
palplot(themes.palettes.Diverging.redgreen)
与熊猫一起使用
import pandas as pd
# some made up date
sales = np.random.randint(low=10, high=20, size=30) * [i**2 for i in range(1, 31)]
revenue = np.random.random(30) * sales
months = pd.date_range(start="2010-01-01", periods=30, freq="M")
df = pd.DataFrame({"sales": sales, "revenue": revenue.round(2)}, index=months)
df.head()
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
</style>
| 销售量 | 收入 | |
|---|---|---|
| 2010-01-31 | 12 | 2.76 |
| 2010-02-28 | 52 | 45.05 |
| 2010-03-31 | 90 | 11.80 |
| 2010-04-30 | 208 | 203.93 |
| 2010-05-31 | 475 | 337.08 |
themes.theme_minimal()
df 。情节()
<matplotlib.axes._subplots.AxesSubplot at 0x7fdc5285f2b0>
themes.theme_dark(palette=themes.palettes.Autumn1.colors)
df.plot()
<matplotlib.axes._subplots.AxesSubplot at 0x7fdc5297e668>
贡献
有多种方法可以帮助您完成这个项目:
- 提交错误报告
- 提交功能请求
- fork 这个 git repo,修改一些代码,然后提交一个 Pull Request
- 添加文档或示例算作更改代码