[英]How to make plots with small whitespace separations in Matplotlib or Seaborn?
actually, i can plot this plot use ggplot2.实际上,我可以使用 ggplot2 绘制此图。 for example:
例如:
head(mtcars) # mpg cyl disp hp drat wt qsec vs am gear carb # Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 # Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 # Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 # Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 # Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 # Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 library(dplyr) library(ggplot2) mtcars %>% reshape2::melt() %>% ggplot(aes(x = variable, y = value)) + geom_point() + facet_grid(~ variable) + theme(axis.text.x = element_blank())
facet_grid(~)
.this function can change your plot into multiple plot by your categrical variable
facet_grid(~)
这个函数可以通过你的分类变量将你的图变成多个图
Here is an approach to draw a similar plot using Python's matplotlib.这是一种使用 Python 的 matplotlib 绘制类似图的方法。 The plot has a grey background and white major and minor gridlines to delimit the zones.
该图具有灰色背景和白色的主要和次要网格线来分隔区域。 Getting the dots in the center of each little cell is somewhat tricky: divide into n+1 spaces and shift half a cell (1/2n).
将点放在每个小单元格的中心有点棘手:分成 n+1 个空格并移动半个单元格 (1/2n)。 A secondary x-axis can be used to set the labels.
辅助 x 轴可用于设置标签。 A
zorder
has to be set to have the dots on top of the gridlines.必须将
zorder
设置为在网格线顶部具有点。
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import ticker
n = 5
cols = 7
values = [np.random.uniform(1, 10, n) for c in range(cols)]
fig, ax = plt.subplots()
ax.set_facecolor('lightgrey')
ax.xaxis.set_major_locator(ticker.MultipleLocator(1))
ax.xaxis.set_minor_locator(ticker.MultipleLocator(1 / (n)))
ax.yaxis.set_major_locator(ticker.MultipleLocator(1))
ax.grid(True, which='both', axis='both', color='white')
ax.set_xticklabels([])
ax.tick_params(axis='x', which='both', length=0)
ax.grid(which='major', axis='both', lw=3)
ax.set_xlim(1, cols + 1)
for i in range(1, cols + 1):
ax.scatter(np.linspace(i, i + 1, n, endpoint=False) + 1 / (2 * n), values[i-1], c='crimson', zorder=2)
ax2 = ax.twiny()
ax2.set_xlim(0.5, cols + 0.5)
ticks = range(1, cols + 1)
ax2.set_xticks(ticks)
ax2.set_xticklabels([f'Cat_{t:02d}' for t in ticks])
bbox = dict(boxstyle="round", ec="limegreen", fc="limegreen", alpha=0.5)
plt.setp(ax2.get_xticklabels(), bbox=bbox)
ax2.tick_params(axis='x', length=0)
plt.show()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.