如何在matplotlib上生成一系列直方图? [英] how to generate a series of histograms on matplotlib?
问题描述
我想生成一系列如下所示的直方图:
I would like to generate a series of histogram shown below:
以上可视化是在tensorflow中完成的,但我想在matplotlib上重现相同的可视化.
The above visualization was done in tensorflow but I'd like to reproduce the same visualization on matplotlib.
使用@SpghttCd建议的plt.fill_between,我有以下代码:
Using plt.fill_between suggested by @SpghttCd, I have the following code:
colors=cm.OrRd_r(np.linspace(.2, .6, 10))
plt.figure()
x = np.arange(100)
for i in range(10):
y = np.random.rand(100)
plt.fill_between(x, y + 10-i, 10-i,
facecolor=colors[i]
edgecolor='w')
plt.show()
这很好用,但是可以使用直方图代替连续曲线吗?
This works great, but is it possible to use histogram instead of a continuous curve?
推荐答案
基于欢乐的方法,就像十月的评论中提到的那样:
joypy based approach, like mentioned in the comment of october:
import pandas as pd
import joypy
import numpy as np
df = pd.DataFrame()
for i in range(0, 400, 20):
df[i] = np.random.normal(i/410*5, size=30)
joypy.joyplot(df, overlap=2, colormap=cm.OrRd_r, linecolor='w', linewidth=.5)
为了更好地控制颜色,您可以定义一个颜色渐变函数,该函数接受小数索引并开始和停止颜色元组:
for finer control of colors, you can define a color gradient function which accepts a fractional index and start and stop color tuples:
def color_gradient(x=0.0, start=(0, 0, 0), stop=(1, 1, 1)):
r = np.interp(x, [0, 1], [start[0], stop[0]])
g = np.interp(x, [0, 1], [start[1], stop[1]])
b = np.interp(x, [0, 1], [start[2], stop[2]])
return (r, g, b)
用法:
joypy.joyplot(df, overlap=2, colormap=lambda x: color_gradient(x, start=(.78, .25, .09), stop=(1.0, .64, .44)), linecolor='w', linewidth=.5)
具有不同的开始和结束元组的示例:
Examples with different start and stop tuples:
原始答案:
您可以使用plt.fill_between
遍历要绘制的数据数组,将颜色设置为某些渐变,将线颜色设置为白色:
You could iterate over your dataarrays you'd like to plot with plt.fill_between
, setting colors to some gradient and the line color to white:
创建一些示例数据:
import numpy as np
t = np.linspace(-1.6, 1.6, 11)
y = np.cos(t)**2
y2 = lambda : y + np.random.random(len(y))/5-.1
绘制序列:
import matplotlib.pyplot as plt
import matplotlib.cm as cm
colors = cm.OrRd_r(np.linspace(.2, .6, 10))
plt.figure()
for i in range(10):
plt.fill_between(t+i, y2()+10-i/10, 10-i/10, facecolor = colors[i], edgecolor='w')
如果您希望针对示例对它进行更优化,则应该考虑提供一些示例数据.
If you want it to have more optimized towards your example you should perhaps consider providing some sample data.
正如我在下面评论的那样,我不确定我是否了解您想要的内容,或者您是否想要最好的解决方案.因此,这里有一个代码,除了您在编辑中的方法外,还绘制了两个如何以更好的可比性方式呈现一堆直方图的示例:
As I commented below, I'm not quite sure if I understand what you want - or if you want the best for your task. Therefore here a code which plots besides your approach in your edit two smples of how to present a bunch of histograms in a way that they are better comparable:
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.cm as cm
N = 10
np.random.seed(42)
colors=cm.OrRd_r(np.linspace(.2, .6, N))
fig1 = plt.figure()
x = np.arange(100)
for i in range(10):
y = np.random.rand(100)
plt.fill_between(x, y + 10-i, 10-i,
facecolor=colors[i],
edgecolor='w')
data = np.random.binomial(20, .3, (N, 100))
fig2, axs = plt.subplots(N, figsize=(10, 6))
for i, d in enumerate(data):
axs[i].hist(d, range(20), color=colors[i], label=str(i))
fig2.legend(loc='upper center', ncol=5)
fig3, ax = plt.subplots(figsize=(10, 6))
ax.hist(data.T, range(20), color=colors, label=[str(i) for i in range(N)])
fig3.legend(loc='upper center', ncol=5)
这导致了以下情节:
-
您从编辑中获得的情节:
your plot from your edit:
N个直方图:
N个直方图在一个图中并排显示:
N histograms side by side in one plot:
这篇关于如何在matplotlib上生成一系列直方图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!