天道酬勤,学无止境

matplotlib

Python Matplotlib - Misaligned Grid Lines and Color Fills

I'm using the following code to produce a sort of binary heatmap: import numpy as np import matplotlib.colors as mlc import matplotlib.pyplot as mlp states = ['AAAA', 'BBBB', 'CCCC', 'DDDD'] walk = [1, 2, 3, 2, 3, 2, 3, 2, 3, 2] states_len = len(states) walk_len = len(walk) img = np.zeros((states_len, walk_len), dtype=float) for i, s in enumerate(walk): img[s, i] = 1.0 figure, ax = mlp.subplots() color_map = mlc.LinearSegmentedColormap.from_list('ColorMap', [(1.000, 1.000, 1.000), (0.984, 0.501, 0.447)]) ax.imshow(img, cmap=color_map, interpolation='none') ax.set_xlabel('Steps', fontsize=13)

2022-01-16 17:55:18    分类:问答    python   python-3.x   matplotlib   plot

How to turn off autoscaling in matplotlib.pyplot

I am using matplotlib.pyplot in python to plot my data. The problem is the image it generates seems to be autoscaled. How can I turn this off so that when I plot something at (0,0) it will be placed fixed in the center?

2022-01-16 17:41:32    分类:问答    python   matplotlib

matplotlib 为单个散点图添加具有多个条目的图例(matplotlib add legend with multiple entries for a single scatter plot)

问题 我正在为如下所示的数据集制作散点图: x = [1, 1, 2, 2, 3, 3, 4, 4] y = [1, 2, 3, 4, 1, 2, 3, 4] labels = [1, 3, 0, 2, 2, 1, 0, 3] colors = np.array(plt.rcParams['axes.prop_cycle'].by_key()['color']) plt.scatter(x, y, color=colors[labels]) 如果我调用plt.legend ,对于整个数据集,将只显示一个条目,并带有第一个符号。 如何创建一个包含所有四个元素的图例,就像我绘制了四个单独的数据集一样显示? 可能相关:具有多个图例条目的 Matplotlib 直方图基于:Matplotlib,如何循环? 回答1 您可以为一组标签绘制空列表: for l in set(labels): plt.scatter([],[], color=colors[l], label=l) plt.legend() 回答2 我认为整体最简单的解决方案是将所有工作委托给 matplotlib。 该方法在此处描述:https://matplotlib.org/gallery/lines_bars_and_markers/scatter_with_legend.html#automated-legend

2022-01-16 17:36:30    分类:技术分享    python   matplotlib   legend

set or peg bivariate distribution values using matplotlib

I've animated a bivariate gaussian distribution using matplotlib. I've calculated this distribution by adjusting the COV matrix to account for specific variables. I can provide greater detail on this process but essentially each scatter point covers a specific displacement that is easily identifiable. My question or problem I'm having is trying to set/fix/peg areas that isn't covered by the distribution. You can see the values oscillating around by the variation in colour. Question: Is it possible to set or peg these neutral areas to a specific value and therefore colour. Specifically, the

2022-01-16 17:29:13    分类:问答    python   pandas   numpy   matplotlib   scipy

How to convert matplotlib animation to an HTML5 <video> tag

Below there is the code of a matplolib animation graph and here is how to save it. from IPython.display import HTML import matplotlib.pyplot as plt import matplotlib.animation import numpy as np t = np.linspace(0,2*np.pi) x = np.sin(t) fig, ax = plt.subplots() ax.axis([0,2*np.pi,-1,1]) l, = ax.plot([],[]) def animate(i): l.set_data(t[:i], x[:i]); ani = matplotlib.animation.FuncAnimation(fig, animate, frames=len(t)); #HTML(ani.to_jshtml()) ani.to_html5_video() What I basically do is copying the generated code into a basic html script like this one: <html> <head> <title>Page Title</title> </head

2022-01-16 17:27:01    分类:问答    python   html   matplotlib

How to use custom error bar in seaborn lineplot

I am using seaborn.lineplot to generate some time series plots. I have pre-compute a specific kind of error bars in two lists, e.g., upper=[1,2,3,4,5] lower=[0,1,2,3,4]. Is there a way I could customize the error bar here, instead of using the CI or Std error bars in lineplot?

2022-01-16 17:22:21    分类:问答    matplotlib   data-visualization   seaborn

matplotlib versions >=3 does not inlclude a find()

I am running a very simple python script: from tftb.generators import amgauss, fmlin I get this error: C:\Users\Anaconda3\envs\tf_gpu\lib\site-packages\tftb-0.0.1-py3.6.egg\tftb\processing\affine.py in <module> 12 13 import numpy as np ---> 14 from matplotlib.mlab import find 15 from scipy.signal import hilbert 16 from scipy.optimize import brenth, newton ImportError: cannot import name 'find' I believe find is no longer in versions >=3. How can I get around this without downgrading Matplotlib?

2022-01-16 17:16:04    分类:问答    python-3.x   matplotlib

为什么有分号; 在 matplotlibs plot() 函数之后?(Why is there a semicolon ; after matplotlibs plot() function?)

问题 我已经看过几次了: # http://geopandas.org/aggregation_with_dissolve.html continents.plot(column = 'pop_est', scheme='quantiles', cmap='YlOrRd'); # https://jakevdp.github.io/PythonDataScienceHandbook/04.14-visualization-with-seaborn.html plt.legend('ABCDEF', ncol=2, loc='upper left'); # https://matplotlib.org/api/pyplot_api.html x = np.arange(0, 5, 0.1); 让我感到困惑的是,它经常用 matplotlib 完成,而且也不一致。 这只是一个错误吗? 它在 matplotlib 文档中有一段时间吗? 做过/做过; 在Python中的一行有什么功能吗? (这表明答案是“否”,但没有解释为什么它在 matplotlib 中如此广泛传播)。 回答1 如链接问题的答案所述,纯python中分号的唯一目的是将语句分隔在一行中。 但是,在这里您会在行尾观察到分号,这在纯 python 中确实没有任何用途。 这里主要有两个原因: 正如@hpaulj 所评论的那样

2022-01-16 15:59:43    分类:技术分享    python   matplotlib

带孔的 Matplotlib 补丁(Matplotlib patch with holes)

问题 以下代码有效。 问题是我不知道它为什么会起作用。 该代码从中心绘制一个带有三角形切口的圆形补丁(使用 PathPatch)。 我的猜测是内三角形被切掉了,因为它是顺时针绘制的,而外圆是逆时针绘制的。 如果方向不反转,三角形就不会被切断。 我在文档中没有找到有关所采用规则的任何内容。 那么为什么会这样呢? from matplotlib import pyplot from matplotlib.path import Path from matplotlib.patches import PathPatch import numpy # # draw a triangle within a circle using PathPatch # phi = numpy.linspace(0, 2*numpy.pi, 100) circle = 4 * numpy.exp(1j * phi) circleV = [[p.real, p.imag] for p in circle] phi = numpy.linspace(0, 2*numpy.pi, 4) triangle = 2 * numpy.exp(1j * phi) triangleV = [[p.real, p.imag] for p in triangle] circleC = [Path.LINETO for p in

2022-01-16 15:36:21    分类:技术分享    python   matplotlib

How to control mouseover text in matplotlib

When you mouseover an image shown with imshow, you can mouseover the image to inspect its RGB values. The bottom-right corner of the matplotlib window (sharing space with the toolbar), shows the image coordinates and RGB values of the pixel being pointed at: x = 274.99 y = 235.584 [.241, .213, .203] However, when I mouseover a quiver plot, it only shows the x and y coords of the pointer, but not the value of the 2D vector being pointed at. Is there a way to get the vector values to show up? I would be fine with writing a custom mouse event handler, if I only knew how to set that bit of text in

2022-01-16 15:07:38    分类:问答    matplotlib   imshow