Categories
Python Answers

How to plot multiple bars with Python matplotlib?

Sometimes, we want to plot multiple bars with Python matplotlib.

In this article, we’ll look at how to plot multiple bars with Python matplotlib.

How to plot multiple bars with Python matplotlib?

To plot multiple bars with Python matplotlib, we can call bar to multiple times.

For instance, we write

import matplotlib.pyplot as plt
from matplotlib.dates import date2num
import datetime

x = [
    datetime.datetime(2021, 1, 4, 0, 0),
    datetime.datetime(2021, 1, 5, 0, 0),
    datetime.datetime(2021, 1, 6, 0, 0)
]
x = date2num(x)

y = [4, 9, 2]
z = [1, 2, 3]
k = [11, 12, 13]

ax = plt.subplot(111)
ax.bar(x-0.2, y, width=0.2, color='b', align='center')
ax.bar(x, z, width=0.2, color='g', align='center')
ax.bar(x+0.2, k, width=0.2, color='r', align='center')
ax.xaxis_date()

plt.show()

to create the x list with some dates.

Then we call date2num with x to convert the dates to values that can be plotted.

Next, we create a subplot with subplot.

And we call ax.bar to plot the bars with diffent x and y values.

We also set the color of the bars to different colors and change the alignment of them by setting align.

And we call xaxis_date to render the x-axis labels as dates.,

Conclusion

To plot multiple bars with Python matplotlib, we can call bar to multiple times.

Categories
Python Answers

How to access a dictionary element in a Django template with Python?

Sometimes, we want to access a dictionary element in a Django template with Python.

In this article, we’ll look at how to access a dictionary element in a Django template with Python.

How to access a dictionary element in a Django template with Python?

To access a dictionary element in a Django template with Python, we can use the items property to get the key-value pairs.

For instance, if we have

choices = {'key1':'val1', 'key2':'val2'}

Then we can render the key-value pairs in the template with

<ul>
{% for key, value in choices.items %} 
  <li>{{key}} - {{value}}</li>
{% endfor %}
</ul>

We get the key-value pairs with choices.items.

And then we render the values with the for loop.

Conclusion

To access a dictionary element in a Django template with Python, we can use the items property to get the key-value pairs.

Categories
Python Answers

How to run Django filter queryset __in for every item in list with Python?

Sometimes, we want to run Django filter queryset __in for every item in list with Python.

In this article, we’ll look at how to run Django filter queryset __in for every item in list with Python.

How to run Django filter queryset __in for every item in list with Python?

To run Django filter queryset __in for every item in list with Python, we can use the itertools.reduce method.

For instance, we write

from operator import and_
from django.db.models import Q

categories = ["holiday", "summer"]
res = Photo.filter(reduce(and_, [Q(tags__name=c) for c in categories]))

to call reduce with the and_ operator and the list of querysets [Q(tags__name=c) for c in categories] to combine them with logical AND.

Then we call filter with the combined query set returned by reduce.

Conclusion

To run Django filter queryset __in for every item in list with Python, we can use the itertools.reduce method.

Categories
Python Answers

How to fix Python app does not print anything when running detached in Docker?

Sometimes, we want to fix Python app does not print anything when running detached in Docker.

In this article, we’ll look at how to fix Python app does not print anything when running detached in Docker.

How to fix Python app does not print anything when running detached in Docker?

To fix Python app does not print anything when running detached in Docker, we run python with the -u flag in our Dockerfile.

For instance, we write

CMD ["python","-u","main.py"]

to run main.py with -u to use unbuffered output.

conclusion

To fix Python app does not print anything when running detached in Docker, we run python with the -u flag in our Dockerfile.

Categories
Python Answers

How to create threads in Python?

Sometimes, we want to create threads in Python.

In this article, we’ll look at how to create threads in Python.

How to create threads in Python?

To create threads in Python, we can use the Thread class.

For instance, we write

from threading import Thread
from time import sleep

def threaded_function(arg):
    for i in range(arg):
        print("running")
        sleep(1)


if __name__ == "__main__":
    thread = Thread(target = threaded_function, args = (10, ))
    thread.start()
    thread.join()

to create a Thread object that runs the threaded_function function with 10 as its argument.

Then we call start to start the thread.

And we call join to block the calling thread until the thread that called join is terminated.

Conclusion

To create threads in Python, we can use the Thread class.