我目前正在尝试绘制一个带有很多刻度的图形。它对每个值都有从 0 到 50 的刻度,导致打包的刻度标签合并,使绘图变得非常混乱。我使用了刻度线
fig, ax = plt.subplots()
ax.set_xticks(np.arange(0,50))
ax.set_yticks(np.arange(0,50))
Run Code Online (Sandbox Code Playgroud)
并使用隐藏刻度标签
ax.set_xticklabels([])
ax.set_yticklabels([])
Run Code Online (Sandbox Code Playgroud)
然而,问题是这隐藏了所有刻度标签,而我希望保留一些标签,以便我可以使用标签轻松跟踪。
我试过做
ax.set_xticklabels(np.arange(0,50,5))
Run Code Online (Sandbox Code Playgroud)
但这只会导致前 10 个刻度使用不适当的标签进行标记(范围(0,50),间隔为 5)
我希望找到一种方法来显示间隔为 5(或任何特定间隔)的刻度“标签”,同时保留所有实际刻度(以便我可以将它们用于网格)。
我可以用这段代码生成以下图:
df_day['Time'] = pd.to_datetime(df_day['Time'], format = '%H:%M:%S')
df_day = df_day.set_index('Time')
fig=plt.figure(dpi=900)
plt.title("TESTER Summary for Day %i " %y + "\n Average Chamber Pressure %.3f [torr] \n" %p_avg_temp + str(dates[x]))
ax1 = df_day['DP-2'].plot(label = 'DP-2')
ax2 = df_day['FM-1'].plot(secondary_y = True, label = "Flow Rate")
ax1.set_ylabel('Temperatures - Pressure Drop\nInlet Pressure - Scale Weight')
ax2.set_ylabel('Flow Rate - Heat Rej.')
ax1.set_xlabel('Time ')
handles,labels = [],[]
for ax in fig.axes:
for h,l in zip(*ax.get_legend_handles_labels()):
handles.append(h)
labels.append(l)
plt.legend(handles, labels, loc = 'lower center', bbox_to_anchor = (0.5, …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用基本 R 并排绘制两个图。
我希望他们分享标题以及 x 轴和 y 轴标签。但是,绘图的范围不同,所以我想保留标签的数量。
我已经看到有一些解决方案使用par()
,但是,它们似乎过于复杂。
我尝试使用以下解决方案layout()
:
dev.off()
# Create a layout with two plots
layout(matrix(c(1, 2), nrow = 1))
# Create the first plot
plot(1:10, 1:10, main = "Plot 1", type = "l", col = "blue", xlab = "")
# Create the second plot without the x-axis
plot(1:10, (1:10)^2, main = "", type = "l", col = "red", xlab = "")
# Draw the x-axis in the middle of the two plots …
Run Code Online (Sandbox Code Playgroud) 我创建了一个pointplot()
,但无法更改 x 轴限制。虽然我的数据只包含 9 个月,但我想在我的轴上显示所有 12 个月。
fig,ax = plt.subplots(figsize=(12,4))
sns.pointplot(data=tr_df, x='Month', y='numOfTrips', hue='Year', ax=ax, palette='nipy_spectral')
# sns.plt.xlim(0, 12) # AttributeError: module 'seaborn' has no attribute 'plt'
# ax.set_xlim=(0, 12) # does nothing
ax.set(xlim=(0, 12))
ax.set(title="Number of trips each month")
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
编辑:用于创建绘图的数据
Year Month numOfTrips
0 2011 7 2608
1 2011 8 33579
2 2011 9 34756
3 2011 10 31423
4 2011 11 20746
5 2012 3 12240
6 2012 4 37637
7 2012 5 46056
8 …
Run Code Online (Sandbox Code Playgroud) 为 1 秒采样数据设置分钟小刻度:OverflowError: int too big to convert
考虑这个数据帧,采样间隔为 1 秒,持续时间约为 30 分钟:
import matplotlib.pyplot as plt
from matplotlib.dates import MinuteLocator
import pandas as pd
ndex = pd.date_range('2021-08-01 07:07:07', '2021-08-01 07:41:12', freq='1S', name='Time')
df = pd.DataFrame(data=np.random.randint(1, 100, len(ndex)), index=ndex, columns=['A'])
Run Code Online (Sandbox Code Playgroud)
现在我们绘制它:
fig, ax = plt.subplots()
df.plot(color='red', marker='x', lw=0, ms=0.2, ax=ax)
Run Code Online (Sandbox Code Playgroud)
现在我希望每分钟都有轻微的滴答声。
我试过这个:
ax.xaxis.set_minor_locator(MinuteLocator())
Run Code Online (Sandbox Code Playgroud)
但这失败了OverflowError: int too big to convert
我正在尝试更改 seaborn 折线图上 x 轴标签的旋转。然而,当我运行下面的代码时,我只得到一个大列表作为输出和一个没有 x 标签的图表:
sns.set(rc={'figure.figsize':(15,5)})
chart = sns.lineplot(y = top_deaths['Deaths'], x = top_deaths['Date'], data = top_deaths, hue ='Country/Region')
x = chart.get_xticklabels()
chart.set_xticklabels(x, rotation = 45)
[Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, 0, ''),
Text(0, …
Run Code Online (Sandbox Code Playgroud) 我尝试使用两次调用 annotate() 的技巧来获取 2 级嵌套刻度标签(此处),但我想与 facet_wrap() 结合使用
df <- tribble(
~action, ~gend, ~status, ~cellMean,
"P", "M", "A", 1,
"P", "M", "B", 2,
"P", "F", "A", 1.4,
"P", "F", "B", 2.6,
"V", "M", "A", 2,
"V", "M", "B", 3,
"V", "F", "A", 2.2,
"V", "F", "B", 3.8
) %>%
mutate(action=factor(action,levels=c("P","V")),
gend =factor(gend, levels=c("F","M")),
status=factor(status,levels=c("A","B")))
df
# action obscured/overlaid
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean,
color=status, shape=gend)) +
geom_point(size=3.5) +
theme_light()
Run Code Online (Sandbox Code Playgroud)
# action used as facet
df %>%
ggplot(data=., mapping=aes(x=interaction(status,gend), y=cellMean, …
Run Code Online (Sandbox Code Playgroud) x-axis ×7
python ×5
matplotlib ×3
pandas ×2
plot ×2
r ×2
seaborn ×2
dataframe ×1
datetime ×1
facet-wrap ×1
ggplot2 ×1
multi-level ×1
xticks ×1
yaxis ×1