我正在处理大数据集,需要绘制多个堆叠趋势图,但遇到了与绘图中允许的 y 轴数量相关的问题。我尝试了多种方法来解决这个问题,但没有效果。
每当我尝试使用 100 多个 y 轴趋势绘制数据图表时,趋势就会开始超越之前的趋势。这是我尝试绘制 106 种趋势的图像:
如果将趋势数量减少到 99 个趋势,则所有这些都会显示在图表中:
我正在使用plotly 的python API 在ipython 笔记本中构建这些图表。
一张图中允许的 y 轴数量是否有限制?
我正在尝试生成温度随时间变化的四面板动画。子图中的四个面板中的每一个都应该是一个动画地图;每个面板之间的差异在于所使用的数据。我已成功使用一组数据(没有子图)和以下代码生成动画:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from mpl_toolkits.basemap import Basemap
#dummy temperature data with 10 time-steps
y=np.random.randn(10, 60, 100)
fig = plt.figure()
m = Basemap(projection='kav7',lon_0=0)
lats=np.linspace(90,-90,y.shape[1])
lons=np.linspace(-180,180,y.shape[2])
lons, lats = np.meshgrid(lons,lats)
m.drawparallels(np.arange(-90.,99.,30.), labels=[1,0,0,0])
m.drawmeridians(np.arange(-180.,180.,60.), labels=[0,0,0,1])
m.drawcoastlines(linewidth=0.25)
m.pcolormesh(lons,lats,y[0],cmap=plt.cm.bwr, shading='flat',latlon=True)
def init():
m
def animate(i):
m.pcolormesh(lons,lats,y[i],cmap=plt.cm.bwr, shading='flat',latlon=True)
return m
anim = animation.FuncAnimation(fig, animate, init_func=init, frames=10, interval=100) #interval = number of milliseconds between frames
anim.save('movie.mp4')
Run Code Online (Sandbox Code Playgroud)
我希望我在正确的地方问这个问题。
我有一个 for 循环,因为创建了许多数字。循环完成后,我想再生成一个图形,其中三个先前创建的图作为子图。
我现在的代码是这样的:
import numpy as np
import matplotlib.pyplot as plt
def f(t):
return np.exp(-t)*np.cos(2*np.pi*t)+(t/10)**2.
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02)
for i in range(2):
fig= plt.figure(i)
ax1=fig.add_subplot(111)
plt.title('Jon Snow')
kraft_plot,=ax1.plot(t1,np.sin(t1),color='purple')
tyrion=ax1.axvline(2,color='darkgreen',ls='dashed')
ax1.set_ylabel('Kraft [N]',color='purple',fontweight='bold')
ax1.set_xlabel('Zeit [s]',fontweight='bold')
ax2=ax1.twinx()
strecke_plot,=ax2.plot(t2,t2/5,color='grey',label='Verlauf der Strecke')
ax2.set_ylabel('Strecke [mm]',color='grey',fontweight='bold')
ax1.legend((kraft_plot,tyrion,strecke_plot),('Jonny','Dwarf','andalltherest'),loc=2)
plt.show()
Run Code Online (Sandbox Code Playgroud)
你能帮助我吗?我可以保存整个图/图吗?
干杯,达洛。
编辑:它应该看起来像这样(右侧部分是我想要实现的): 右侧的文本应该是正常的,显然......
问题是,我首先想要单独打印这些数字,然后一起打印(最后我想保存为 pdf/png,其中包含三个数字)
我使用plotly 中的subplot 函数在我的R-shiny 应用程序中绘制两个图表(一个在另一个下)。图例对于这两个图来说都很常见,这意味着我只有一列将所有图例组合在一起。
我想在子图中将每个图表附近的图例分开。我怎样才能获得它?
p1 <-
iris%>%
group_by(Species)%>%
plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
add_markers(y= ~Sepal.Width)
p2 <-
iris%>%
group_by(Species)%>%
plot_ly(x=~Sepal.Length, color= ~Species, legendgroup=~Species)%>%
add_markers(y= ~Sepal.Width)
subplot(p1,p2, nrows = 2, shareX = T, shareY = F, titleX = T, titleY = T)
Run Code Online (Sandbox Code Playgroud)
更新:我现在得到了正确位置的子图并显示了正确的数据!然而,还有一个问题,那就是我不能给子图自己的标题和颜色条。实际上,我宁愿在所有图的右侧只有一个颜色条,但这似乎也不起作用。这是我的新代码以及一些示例数据(非常没有洞察力,但仅用于测试目的):
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as colors
import cartopy.crs as ccrs
lons = np.arange(-180,180,1)
lats = np.arange(-80,80,1)
dens = np.zeros((12, len(lons), len(lats)))
for i in range(12):
dens[i,:,:] = i
def plotTitle(yr):
letter = chr(yr-2002+97)
return '(' + letter + ') ' + str(yr)
def DensityPlot(yr, lon, lat, dens, ax):
Lat, Lon = np.meshgrid(lat, lon)
density = ax.pcolormesh(Lon, Lat, dens, cmap = 'jet')
#cbar = ax.colorbar(density, orientation='vertical', shrink=0.5, extend='both') # this gives an error: …Run Code Online (Sandbox Code Playgroud) 我正在尝试对两个图进行编码,使一个图位于另一个图的下方。然而,我的代码不断将我的两个图相互对齐。这是我的代码:
import numpy as np
from scipy.integrate import odeint
from numpy import sin, cos, pi, array
import matplotlib
from matplotlib import rcParams
import matplotlib.pyplot as plt
from pylab import figure, axes, title, show
import xlsxwriter
plt.style.use('ggplot')
def deriv(z, t):
l = 0.25 #unextended length of the spring, in m
m = 0.25 #mass of the bob, in kg
k = 29.43 #spring constant, in Nm^-1
g = 9.81 #gravitational acceleration, in ms^-2
x, y, dxdt, dydt = z
dx2dt2 = …Run Code Online (Sandbox Code Playgroud)我正在尝试创建一个带注释的 Sankey 图。我希望最终版本沿着这个手动注释的图表的方向看:
获取桑基图的简单部分:
sankey_diagram <- plot_ly(
type = "sankey",
orientation = "h",
node = list(
label = c("Node_A_1", "Node_A_2", "Node_B_2", "Node_B_3", "Node_C_1", "Node_C_2"),
color = c("blue", "blue", "blue", "blue", "blue", "blue"),
pad = 15,
thickness = 35,
line = list(
color = "black",
width = 0.5
)
),
link = list(
source = c(0,1,0,2,3,3),
target = c(2,3,3,4,4,5),
value = c(8,4,2,8,4,2)
)
) %>%
layout(
font = list(
size = 15
)
)
Run Code Online (Sandbox Code Playgroud)
起初我想,如果我想获得带注释的“列”,我应该转向 plotly 文档的注释部分。注释的问题在于它们在空间上仅限于(至少我认为是)图形的区域。这是基于注释的方法中的代码:
# properties that hide …Run Code Online (Sandbox Code Playgroud) 将多个黄砖图表放入子图排列时遇到问题。标题和图例仅显示最后一个图表。我尝试了多种编写代码的方法,但无法让所有方法都显示图例和标题。我相信上班很简单。
这是一段代码:
f, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2,figsize=(14, 10))
viz = FeatureImportances(LinearRegression(), ax=ax1)
viz.fit(X_train, y_train)
viz = LearningCurve(LinearRegression(), scoring='r2',cv=10, ax=ax2)
viz.fit(X_train, y_train)
viz = ResidualsPlot(clf, ax=ax3)
viz.fit(X_train, y_train)
viz = PredictionError(LinearRegression(), ax=ax4)
viz.fit(X_train, y_train)
viz.score(X_test, y_test)
viz.poof()
Run Code Online (Sandbox Code Playgroud)
我想用左上角的字母(A,B,C,...)来标记子图,这些字母要么与 ylabels 对齐,要么与子图的实际角(而不是轴)对齐。
如果子图的宽度不同,我不能再使用轴变换坐标中带有偏移的文本,因为这会导致每个子图的距离不同。所以我通常使用偏移变换。但如果 ylabel 具有不同的宽度,这将不再起作用。我目前还关注每个图的偏移量,但这还不够好。
这是我到目前为止所拥有的:
import matplotlib.pyplot as plt
from matplotlib import transforms
import numpy as np
fig = plt.figure(figsize=(5, 2), constrained_layout=True)
gs = fig.add_gridspec(1, 3, width_ratios=[1, 2, 3])
axes = [fig.add_subplot(gs[i]) for i in range(3)]
ylabels = ["flat label", "bigger\nlabel", "even\nbigger\nlabel"]
labels = ["A", "B", "C"]
scaledtrans = transforms.ScaledTranslation(-0.4, 0, fig.dpi_scale_trans)
for ax, ylabel, label in zip(axes, ylabels, labels):
ax.set_ylabel(ylabel)
ax.text(0, 1, label, fontsize=12, fontweight="bold", va="bottom", ha="left",
transform=ax.transAxes + scaledtrans)
Run Code Online (Sandbox Code Playgroud)
如下图所示,标签与轴脊的距离相同,但我希望它们与 ylabel 的左边缘或子图的角对齐,在这种情况下,它们与 ylabel 水平重合。但没有 transSubplot,只有 transAxes …
我正在尝试绘制三张图像,其字幕位于其上方,但现在没有显示任何内容。
import cv2
from matplotlib import pyplot as plt
img = cv2.imread('kids.tif')
# Averaged environment
avg_blur = cv2.blur(img,(5,5))
# Gaussian filter
gauss_blur = cv2.GaussianBlur(img,(0,0),5)
# Median filter
median_blur = cv2.medianBlur(img,5)
f, axarr = plt.subplots(nrows=1,ncols=3)
axarr[0].imshow(img)
axarr[1].imshow(avg_blur)
axarr[2].imshow(gauss_blur)
"""
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(avg_blur),plt.title('Averaged environment')
plt.xticks([]), plt.yticks([])
plt.show()
"""
Run Code Online (Sandbox Code Playgroud)