这是我尝试的代码:
@Override
public void handleEvent(Event event) {
System.out.println("THIS IS ME: HEADER EVENT HANDLER STARTED.....");
PdfDocumentEvent documentEvent = (PdfDocumentEvent) event;
PdfDocument pdfDoc = documentEvent.getDocument();
PdfPage page = documentEvent.getPage();
Rectangle pageSize = page.getPageSize();
int pageNumber = pdfDoc.getPageNumber(page);
String logoImagePath = null;
ClassLoader classLoader = getClass().getClassLoader();
logoImagePath = classLoader.getResource("/Images/logo.png").getPath();
System.out.println("Page size: " + pageSize.getHeight());
Rectangle rectangle = new Rectangle(pageSize.getLeft() + 30, pageSize.getHeight() - 114, pageSize.getWidth() - 60, 80);
PdfCanvas pdfCanvas = new PdfCanvas(page.newContentStreamBefore(), page.getResources(), pdfDoc);
pdfCanvas.rectangle(rectangle);
pdfCanvas.setFontAndSize(FontsAndStyles.getRegularFont(), 10);
Canvas canvas = new Canvas(pdfCanvas, pdfDoc, rectangle);
Div header = new Div();
header.setBorder(Border.NO_BORDER);
try {
Image logoImage = new Image(ImageDataFactory.create(logoImagePath));
logoImage.setFixedPosition(15, 740);
logoImage.scale(.15f, .15f);
header.add(logoImage);
} catch (MalformedURLException ex) {
ex.printStackTrace();
System.err.println(ex.getMessage());
}
Paragraph paragraph = new Paragraph();
Text text = new Text("\n\n\n");
paragraph.add(text);
paragraph.setFont(FontsAndStyles.getRegularFont());
paragraph.setFontSize(8);
text = new Text("http://www.histopath.gr\n");
paragraph.setTextAlignment(TextAlignment.RIGHT);
paragraph.add(text);
header.add(paragraph);
paragraph = new Paragraph();
text = new Text("Αποκορώνου 66, ΧΑΝΙΑ, Τηλ: 2821002827, Κιν: 6948571893, 6976800330, email: histologypathology@gmail.com");
text.setFont(FontsAndStyles.getRegularFont());
text.setFontSize(8);
paragraph.add(text);
paragraph.setTextAlignment(TextAlignment.JUSTIFIED_ALL);
header.add(paragraph);
header.setTextAlignment(TextAlignment.CENTER);
canvas.add(header);
canvas.setBorder(Border.NO_BORDER);
canvas.close();
}
它以前没有任何问题地发出了禁令。但是不能使用文本。我收到以下错误:
import matplotlib.pyplot as plt
import numpy as np
bb = [1,2,3,4,5,6,7,8,9,10]
cc = ["red","red","yellow","red","green","red","red","green","red","red"]
tt = ["\u2714","\u2714""\u2718","\u2714","\u2718","\u2714","\u2714","\u2718","\u2714","\u2714"]
x1=np.arange(10)
x2=np.arange(10)
fig = plt.figure()
fig.set_size_inches(50,70)
ax1 = fig.add_subplot(331)
ax1.bar(np.arange(len(bb)), bb, color=cc,width=0.6)
text_applied = ax1.text(x1,2,tt,color=cc)
plt.show()
请让我知道如何根据指定的颜色和Traceback (most recent call last):
File "C:\Python35\lib\tkinter\__init__.py", line 1549, in __call__
return self.func(*args)
File "C:\Python35\lib\tkinter\__init__.py", line 596, in callit
func(*args)
File "C:\Python35\lib\site-packages\matplotlib\backends\_backend_tk.py", line 310, in idle_draw
self.draw()
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 12, in draw
super(FigureCanvasTkAgg, self).draw()
File "C:\Python35\lib\site-packages\matplotlib\backends\backend_agg.py", line 433, in draw
self.figure.draw(self.renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\figure.py", line 1475, in draw
renderer, self, artists, self.suppressComposite)
File "C:\Python35\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\axes\_base.py", line 2607, in draw
mimage._draw_list_compositing_images(renderer, self, artists)
File "C:\Python35\lib\site-packages\matplotlib\image.py", line 141, in _draw_list_compositing_images
a.draw(renderer)
File "C:\Python35\lib\site-packages\matplotlib\artist.py", line 55, in draw_wrapper
return draw(artist, renderer, *args, **kwargs)
File "C:\Python35\lib\site-packages\matplotlib\text.py", line 706, in draw
bbox, info, descent = textobj._get_layout(renderer)
File "C:\Python35\lib\site-packages\matplotlib\text.py", line 282, in _get_layout
key = self.get_prop_tup(renderer=renderer)
File "C:\Python35\lib\site-packages\matplotlib\text.py", line 862, in get_prop_tup
x, y = self.get_unitless_position()
File "C:\Python35\lib\site-packages\matplotlib\text.py", line 844, in get_unitless_position
x = float(self.convert_xunits(self._x))
TypeError: only size-1 arrays can be converted to Python scalars
显示文本。
答案 0 :(得分:3)
我不确定您的预期输出是什么,但是如果您想在每个小节下方使用不同颜色的符号,可以将其设置为刻度线,如下所示:
import matplotlib.pyplot as plt
import numpy as np
bb = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
cc = ["red", "red", "yellow", "red", "green", "red", "red", "green", "red", "red"]
tt = ["\u2714", "\u2714", "\u2718", "\u2714", "\u2718", "\u2714", "\u2714", "\u2718", "\u2714", "\u2714"]
fig = plt.figure()
ax1 = fig.add_subplot(111)
ax1.bar(range(len(bb)), bb, color=cc, width=0.6)
ax1.set_xticks(range(len(bb)))
ax1.set_xticklabels(tt)
for xtick, color in zip(ax1.get_xticklabels(), cc):
xtick.set_color(color)
plt.show()
输出:
顺便说一句,请注意,您的代码tt
中第二个和第三个元素之间缺少逗号。
答案 1 :(得分:2)
我不太明白你的问题。在plt.text()
的文档中,我找不到将字符串数组馈入参数的可能性:
定义:文本(x,y,s,fontdict = None,withdash = False,** kwargs)
类型:matplotlib.pyplot模块的功能
向轴添加文本。
将文本s添加到数据坐标中x,y位置的轴上。
参数
x,y:
标量放置文本的位置。默认情况下,这是在 数据坐标。可以使用 转换参数。
s:str文本。
所以我建议简单
for x, t, c in zip(x1, tt, cc):
ax1.text(x, 2, t, color=c)