我正在尝试创建一系列x,y对的散点图,其中每个系列都有不同的颜色。我的输入是一个形状为2x3x10的三维numpy数组。换句话说:三组不同的10 x,y对。这个最小的例子从双变量正态分布生成对,但正如你所看到的那样,我在绘制系列时的尝试要么导致系列之间没有颜色区分,要么最终得到许多不同的颜色(也许是10个?)。为了清楚起见,我想以三种不同的颜色结束,一组用于三组10 x,y对,或者每个切片通过阵列的第三维一种颜色。
我应该怎么做呢?是否有不同的方法来定义matplotlib的颜色?或者我应该更早地改变事物并定义多个二维数组而不是3D数组?我对编程很新,对numpy和matplotlib来说更是如此,但是从我收集的内容来看,利用数组的多维度来组织事物是一种很好的做法,但是对这方面的任何其他一般指导都很感激。
pairs = np.random.multivariate_normal((1,5),[[1,0],[0,1]],(10,3)).T
array([[[ 0.49358789, 0.57551098, 2.7029197 , 0.9437744 , -0.45122972,
0.05786102, 1.76313729, -0.72469019, 0.53466069, 0.67888213],
[ 2.88773234, 1.43831903, -0.7427195 , -0.01451867, 1.56491086,
1.72596764, 1.3953636 , 1.67816112, 0.02839967, 0.96014133],
[ 2.52065319, -0.2485202 , 1.51877564, 2.31216588, 1.35005209,
1.30100189, 0.63590115, 0.32281779, 2.14906114, 0.1551461 ]],
[[ 4.85695486, 6.06754 , 5.93342725, 3.49327716, 6.69661302,
6.52707216, 4.61195227, 3.22767035, 4.23710242, 7.19532735],
[ 5.06087316, 4.29734169, 5.66389379, 4.60574012, 4.96619091,
4.88981834, 3.65294396, 5.65582142, 6.27162773, 6.67958156],
[ 5.47524034, 4.8989236 , 3.96246028, 6.31088811, 5.39779792,
5.67488569, 4.66692489, 4.17364195, 3.69659271, 5.85626402]]])
# Note that the actual graphics were based on another random sample
# than the one listed above, due to a mistake on my end.
plt.plot(pairs[0],pairs[1],'x');plt.show()
plt.scatter(pairs[0],pairs[1]);plt.show()
答案 0 :(得分:1)
/// <summary>
/// Provides an IProgress{T} that invokes callbacks for each reported progress value.
/// </summary>
/// <typeparam name="T">Specifies the type of the progress report value.</typeparam>
/// <remarks>
/// Any handler provided to the constructor or event handlers registered with
/// the <see cref="ProgressChanged"/> event are invoked through a
/// <see cref="System.Threading.SynchronizationContext"/> instance captured
/// when the instance is constructed. If there is no current SynchronizationContext
/// at the time of construction, the callbacks will be invoked on the ThreadPool.
/// </remarks>
public class SynchronousProgress<T> : IProgress<T>
{
/// <summary>The synchronization context captured upon construction. This will never be null.</summary>
private readonly SynchronizationContext m_synchronizationContext;
/// <summary>The handler specified to the constructor. This may be null.</summary>
private readonly Action<T> m_handler;
/// <summary>A cached delegate used to post invocation to the synchronization context.</summary>
private readonly SendOrPostCallback m_invokeHandlers;
/// <summary>Initializes the <see cref="Progress{T}"/>.</summary>
public SynchronousProgress()
{
// Capture the current synchronization context. "current" is determined by Current.
// If there is no current context, we use a default instance targeting the ThreadPool.
m_synchronizationContext = SynchronizationContext.Current ?? ProgressStatics.DefaultContext;
Contract.Assert(m_synchronizationContext != null);
m_invokeHandlers = new SendOrPostCallback(InvokeHandlers);
}
/// <summary>Initializes the <see cref="Progress{T}"/> with the specified callback.</summary>
/// <param name="handler">
/// A handler to invoke for each reported progress value. This handler will be invoked
/// in addition to any delegates registered with the <see cref="ProgressChanged"/> event.
/// Depending on the <see cref="System.Threading.SynchronizationContext"/> instance captured by
/// the <see cref="Progress"/> at construction, it's possible that this handler instance
/// could be invoked concurrently with itself.
/// </param>
/// <exception cref="System.ArgumentNullException">The <paramref name="handler"/> is null (Nothing in Visual Basic).</exception>
public SynchronousProgress(Action<T> handler) : this()
{
if (handler == null) throw new ArgumentNullException("handler");
m_handler = handler;
}
/// <summary>Raised for each reported progress value.</summary>
/// <remarks>
/// Handlers registered with this event will be invoked on the
/// <see cref="System.Threading.SynchronizationContext"/> captured when the instance was constructed.
/// </remarks>
public event EventHandler<T> ProgressChanged;
/// <summary>Reports a progress change.</summary>
/// <param name="value">The value of the updated progress.</param>
protected virtual void OnReport(T value)
{
// If there's no handler, don't bother going through the [....] context.
// Inside the callback, we'll need to check again, in case
// an event handler is removed between now and then.
Action<T> handler = m_handler;
EventHandler<T> changedEvent = ProgressChanged;
if (handler != null || changedEvent != null)
{
// Post the processing to the [....] context.
// (If T is a value type, it will get boxed here.)
m_synchronizationContext.Send(m_invokeHandlers, value);
}
}
/// <summary>Reports a progress change.</summary>
/// <param name="value">The value of the updated progress.</param>
void IProgress<T>.Report(T value) { OnReport(value); }
/// <summary>Invokes the action and event callbacks.</summary>
/// <param name="state">The progress value.</param>
private void InvokeHandlers(object state)
{
T value = (T)state;
Action<T> handler = m_handler;
EventHandler<T> changedEvent = ProgressChanged;
if (handler != null) handler(value);
if (changedEvent != null) changedEvent(this, value);
}
}
/// <summary>Holds static values for <see cref="Progress{T}"/>.</summary>
/// <remarks>This avoids one static instance per type T.</remarks>
internal static class ProgressStatics
{
/// <summary>A default synchronization context that targets the ThreadPool.</summary>
internal static readonly SynchronizationContext DefaultContext = new SynchronizationContext();
}