graphviz.sources.Source怎样调整图片大小
时间: 2023-02-07 21:30:15 浏览: 64
可以使用 `size` 参数来调整图片大小。例如:
```python
from graphviz import Source
source = Source('''
graph {
A -- B
}
''', format='png', size='4,4')
source.render('test-output/test-size')
```
这将生成一个 4x4 英寸的图片。
相关问题
graphviz.sources.Source at 0x1b7181719a0
`graphviz.sources.Source` is a class from the Graphviz Python library used to represent and generate source code for Graphviz graphs. It is typically used in conjunction with the `graphviz.Digraph` class to create and render graphs in various formats.
Here is an example of using `graphviz.Digraph` and `graphviz.sources.Source` to create and render a simple directed graph:
```python
from graphviz import Digraph, Source
# create a new directed graph
dot = Digraph()
# add some nodes and edges
dot.node('A')
dot.node('B')
dot.node('C')
dot.edge('A', 'B')
dot.edge('B', 'C')
# render the graph as a PNG image
dot.format = 'png'
dot.render('example')
# generate the source code for the graph
src = Source(dot.source)
# print the source code
print(src)
```
The output of the above code would be the source code for the graph in a Graphviz DOT file format. It can be used to render the graph in other formats, such as PDF or SVG, by using the `dot.render` method with the appropriate file extension.
怎么保存 graphviz.Source的文件
你可以使用Graphviz包中的render函数将graphviz.Source对象保存为图片或PDF文件。下面是一个保存为PNG格式的示例代码:
```python
from graphviz import Source
# 创建一个graphviz.Source对象
graph = Source('digraph {A -> B; B -> C; C -> A}')
# 保存为PNG格式的文件
graph.render('example', format='png')
```
在这个示例中,`graph.render()`函数将Source对象保存为名为“example.png”的PNG文件。你还可以使用其他格式,例如PDF、SVG等。