Vertical axis title not correctly drawn
The vertical axis title is not always correctly drawn (some characters seem to be rotated, incorrectly placed and are not in bold) when other text actors (such as a vtkCornerAnnotation
) are rendered. Steps to reproduce (see also code below):
- Create a
vtkChartXY
with custom y title. - Add a
vtkCornerAnnotation
. - Create a
vtkChartXY
with custom y title, different from the title in step 1.
If step 2 is omitted (no other text actors drawn), everything works fine. If in step 3 the same y title is used as in step 1, everything works fine as well.
Python code to reproduce
import vtk
from vtkmodules.util.numpy_support import vtk_to_numpy
from PIL import Image
width, height = 400, 400
def create_renderer():
renwin = vtk.vtkRenderWindow()
renwin.SetOffScreenRendering(1)
renwin.SetSize(width, height)
renderer = vtk.vtkRenderer()
renderer.SetBackground([0.6, 1.0, 0.7])
renderer.SetBackgroundAlpha(1.0)
renwin.AddRenderer(renderer)
return renwin, renderer
def screenshot(renwin, name):
# Save offscreen rendering to image:
renwin.SetMultiSamples(0)
renwin.Render()
arr = vtk.vtkUnsignedCharArray()
renwin.GetRGBACharPixelData(0, 0, width - 1, height - 1, 0, arr)
data = vtk_to_numpy(arr).reshape(height, width, -1)[::-1]
Image.fromarray(data).save(f"{name}.png")
def plot_chart(renderer, title_y):
chart_scene = vtk.vtkContextScene()
chart_actor = vtk.vtkContextActor()
chart_actor.SetScene(chart_scene)
renderer.AddActor(chart_actor)
chart_scene.SetRenderer(renderer)
chart = vtk.vtkChartXY()
chart_scene.AddItem(chart)
table = vtk.vtkTable()
arrX = vtk.vtkFloatArray()
arrX.SetName("X axis")
arrL = vtk.vtkFloatArray()
arrL.SetName("Line")
table.AddColumn(arrX)
table.AddColumn(arrL)
numPoints = 5
table.SetNumberOfRows(numPoints)
for i in range(numPoints):
table.SetValue(i, 0, i)
table.SetValue(i, 1, i*i)
plot = chart.AddPlot(vtk.vtkChart.LINE)
plot.SetInputData(table, 0, 1)
chart.GetAxis(vtk.vtkAxis.LEFT).SetTitle(title_y)
def plot_text(renderer, text):
actor = vtk.vtkCornerAnnotation()
actor.SetText(vtk.vtkCornerAnnotation.UpperLeft, text)
actor.GetTextProperty().SetColor(0.0, 0.0, 0.0)
renderer.AddActor(actor)
# Step 1: Create first chart
renwin1, renderer1 = create_renderer()
plot_chart(renderer1, "Label before")
screenshot(renwin1, "chart_before")
# Step 2: Create renderer with text actor
renwin2, renderer2 = create_renderer()
plot_text(renderer2, "Foo")
screenshot(renwin2, "plotter_text")
# Step 3: Create second chart *with different y title*
renwin3, renderer3 = create_renderer()
plot_chart(renderer3, "Custom after")
screenshot(renwin3, "chart_after")
Possibly related stale issue: #17417 (closed)
Any pointers to what could be causing this would be helpful as well. And if it's a cache issue, is there a way to clear this cache?