Skip to main content

Posts

Showing posts from July, 2022

Stdout on Google Colab Notebook in Python

How can I turn stdout to the terminal back, when use 'sys.stdout' on Google Colab Notebook in Python. import sys print(sys.__stdout__) print(sys.stdout) # <_io.TextIOWrapper name=' ' mode='w' encoding='UTF-8'> # <ipykernel.iostream.OutStream object at 0x7f1042f742d0> Those are differen. Once output to a file, the system does not turn to the original output system easily. Recommend that first the original output is stored to a variable. temp_stdout = sys.stdout Finally you can change the stdout to a file. sys.stdout = open('out.txt', 'w') print('Output to the file.') sys.stdout = sys.__stdout__ print('Where is output?') sys.stdout = temp_stdout print('Output to the terminal')

Draw a Common Emitter Amplifier with SchemDraw on Python3.7

I tried to draw a common emitter amplifier by using SchemDraw package in Google Colab Notebook. I installed SchemDraw version=0.14 by pip in Colab Notebook. This version is not latest. The latest version(=0.15) probably has some ploblem. pip install schemdraw==0.14 The version of Python in the notebook kernel is 3.7. It came up against a diffculty. In Python 3.7. or earlier the Walrus operator := is not available. import schemdraw print('Schemdraw',schemdraw.__version__) !python --version # Schemdraw 0.14 # Python 3.7.13 Sample codes in the SchemDraw Gallary are written with the Walrus operator. For instance the code below is one in the gallary and dose not work in the Colab Notebook. import schemdraw import schemdraw.elements as elm # Discharging capacitor with schemdraw.Drawing() as d: d += (V1 := elm.SourceV().label('5V')) d += elm.Line().right(d.unit*.75) d += (S1 := elm.SwitchSpdt2(action='close').up().anchor('b').label('$t=0$...

Schemdraw AttributeError: 'Text' object has no property 'math_fontfamily'

When I used Method label of schemdraw.elements.xxxx, I got an error message ERROR: AttributeError: 'Text' object has no property 'math_fontfamily'. This error happened in matplotlib. The property 'math_fontfamily' may be changed to 'mathtext.fontset'. But the ploblem was the version of Schemdraw that is 0.15. pip install schemdraw==0.15 import schemdraw.elements as elm d = schemdraw.Drawing() d += elm.Resistor().label('6.8KΩ') d.draw() # AttributeError: 'Text' object has no property 'math_fontfamily' When I installed version=0.14, no error appeared. pip install schemdraw==0.14 import schemdraw.elements as elm d = schemdraw.Drawing() d += elm.Resistor().label('6.8KΩ') d.draw() With version=0.14, no more error.

Draw high-quality electrical circuit schematic diagrams with Schemdraw in Python

Here I will draw electrical circuit diagrams with Schemdraw in Python. Schemdraw is a Python package for producing high-quality electrical circuit schematic diagrams. Circuit elements are added, one at a time, similar to how you might draw them by hand, using Python methods. Installation Schemdraw can be installed from pip using pip install schemdraw The schemdraw module "schemdraw.elements" is used for drawing circuit elements. It contains Basic Elements pre-defined for use in a drawing. import schemdraw import schemdraw.elements as elm You can create with using a schemdraw.Drawing and add elements using 'add'method or '+=' poperator. with schemdraw.Drawing() as d: d.add(elm.Resistor()) d.add(elm.Capacitor()) d += elm.Diode() Save figure to a file. Using the with block the drawing is displayed and saved as exiting the with block. The filename is assgined by parameter file in schemdraw.Drawing() like file='my_circui...

DOT Language with Graphviz to render in Pyhton

Graphviz gives two main classes Digraph and Graph that create undirected and directed graphs respectively. They can be rendered and displayed directly inside a notebook. The default formate is PDF in the rendering. See Graphviz Doc . Graph and Digraph objects have a _repr_mimebundle_() method so they can be rendered and displayed directly inside a Jupyter notebook. For an example, check the examples/graphviz-notebook.ipynb file in the source repository/distribution (or the same notebook in nbviewer). import graphviz g = graphviz.Digraph('G', filename='hello.gv') g.edge('Hello', 'World') g Check the generated DOT source code. print(g.source) # digraph G { # Hello -> World # } To use a different output file format you can use the format argument when creating your Graph or Digraph object, or change the format attribute on an existing graph object . When you need a image in 'png', set format='png' or use attribute as g.f...

Built-in type set and Set operations

Python provides a built-in data type set for handling sets. from matplotlib_venn import venn3 # Create set objects: {}, set() A = {2, 4, 6, 8, 10, 12} B = {3, 6, 9, 12, 15, 18} C = {5, 10, 15, 20, 25} # Draw Venn Diagram with matplotlib-venn v = venn3([A, B, C], set_labels = ('Set A', 'Set B', 'Set C'), set_colors=('gray', 'gray', 'gray')) Mathematical operations: # Intersection: &, intersection() # A∩B print(set.intersection(A, B)) # {12, 6} # Custom the Venn Diagram v.get_patch_by_id('110').set_color('red') plt.show() end