📜  Python程序显示心脏上的任何消息

📅  最后修改于: 2022-05-13 01:55:07.440000             🧑  作者: Mango

Python程序显示心脏上的任何消息

这篇文章的重点是讨论如何用美丽的信息画一颗心给你所爱的人,表达你对他们的感受。这里将讨论上述问题陈述的三种变体:

  • 带有消息的红色心形。
  • 带有消息的粉红色红色心形。
  • Pink Heart 带有与上述两种情况不同的字体和颜色的消息。

程序1:下面是一个简单的红心的Python代码,并带有一条消息:

Python3
# Python3 program for the above approach
# import library
import pylab
import numpy as np
  
# Width of heart
Xaxis = np.linspace(-2, 2, 
                    100000)
  
# Setting upper y 
Y1axis = np.sqrt(1 - 
         (abs(Xaxis) - 1) **2)
  
# Setting -y
Y2axis = -3 * np.sqrt(1 - 
         (abs(Xaxis) / 2) **0.5)
  
# Adjust colour for upper part 
# of herat
pylab.fill_between(Xaxis, Y1axis, 
                   color = 'red')
  
# Adjust colour for lower part 
# of heart
pylab.fill_between(Xaxis, Y2axis, 
                   color = 'red')
  
  
pylab.xlim([-2.5, 2.5])
pylab.axis("off")
  
# Driver Code
text = "Geeksforgeeks"
  
pylab.text(0, -0.4, text, 
           fontsize = 24, 
           fontweight = 'bold',
           color = 'white', 
           horizontalalignment = 'center')


Python3
# Python3 program for the above approach
import pylab
import numpy as np
  
# Width of heart
Xaxis = np.linspace(-2, 2, 
                    100000)
  
# Setting upper y 
Y1axis = np.sqrt(1 - 
         (abs(Xaxis) - 1) **2)
  
# Setting -y
Y2axis = -3 * np.sqrt(1 - 
         (abs(Xaxis) / 2) **0.5)
  
# Adjust colour for upper part
# of herat
pylab.fill_between(Xaxis, Y1axis, 
                   color = 'pink')
  
# Adjust colour for lower part 
# of heart
pylab.fill_between(Xaxis, Y2axis, 
                   color = 'red')
  
pylab.xlim([-2.5, 2.5])
pylab.axis("off")
  
# Driver Code
text = "Geeksforgeeks"
  
pylab.text(0, -0.6, text, 
           fontsize = 24, 
           fontweight = 'bold',
           color = 'grey', 
           horizontalalignment = 'center')


Python3
# Python3 program for the above approach
import pylab
import numpy as np
  
# Width of heart
Xaxis = np.linspace(-2, 2, 
                    100000)
  
# Setting upper y 
Y1axis = np.sqrt(1 - 
         (abs(Xaxis) - 1) **2)
  
# Setting lower -y
Y2axis = -3 * np.sqrt(1 - 
         (abs(Xaxis) / 2) **0.5)
   
# Adjust colour for upper part 
# of herat
pylab.fill_between(Xaxis, Y1axis,
                   color = 'pink')
  
# Adjust colour for lower part of heart
pylab.fill_between(Xaxis, Y2axis, 
                   color = 'pink')
  
pylab.xlim([-2.5, 2.5])
pylab.axis("off")
  
# Driver Code
text = "Geeksforgeeks - \n Best platform to explore"
  
# Explore your change here
pylab.text(0, -0.6, text, 
           fontsize = 14, 
           fontweight = 'bold',
           color = 'red', 
           horizontalalignment = 'center')
  
# To save the above image, execute the 
# line- pylab.savefig('heart.png')


输出:

输出#1



程序2:下面是将心脏的一部分颜色更改为粉红色,心脏的其余部分为红色并在心脏上打印一条消息的Python代码:

蟒蛇3

# Python3 program for the above approach
import pylab
import numpy as np
  
# Width of heart
Xaxis = np.linspace(-2, 2, 
                    100000)
  
# Setting upper y 
Y1axis = np.sqrt(1 - 
         (abs(Xaxis) - 1) **2)
  
# Setting -y
Y2axis = -3 * np.sqrt(1 - 
         (abs(Xaxis) / 2) **0.5)
  
# Adjust colour for upper part
# of herat
pylab.fill_between(Xaxis, Y1axis, 
                   color = 'pink')
  
# Adjust colour for lower part 
# of heart
pylab.fill_between(Xaxis, Y2axis, 
                   color = 'red')
  
pylab.xlim([-2.5, 2.5])
pylab.axis("off")
  
# Driver Code
text = "Geeksforgeeks"
  
pylab.text(0, -0.6, text, 
           fontsize = 24, 
           fontweight = 'bold',
           color = 'grey', 
           horizontalalignment = 'center')

输出:

输出#2

程序3:下面是用不同字体大小和颜色的消息来实现粉红色心形的Python代码:

蟒蛇3

# Python3 program for the above approach
import pylab
import numpy as np
  
# Width of heart
Xaxis = np.linspace(-2, 2, 
                    100000)
  
# Setting upper y 
Y1axis = np.sqrt(1 - 
         (abs(Xaxis) - 1) **2)
  
# Setting lower -y
Y2axis = -3 * np.sqrt(1 - 
         (abs(Xaxis) / 2) **0.5)
   
# Adjust colour for upper part 
# of herat
pylab.fill_between(Xaxis, Y1axis,
                   color = 'pink')
  
# Adjust colour for lower part of heart
pylab.fill_between(Xaxis, Y2axis, 
                   color = 'pink')
  
pylab.xlim([-2.5, 2.5])
pylab.axis("off")
  
# Driver Code
text = "Geeksforgeeks - \n Best platform to explore"
  
# Explore your change here
pylab.text(0, -0.6, text, 
           fontsize = 14, 
           fontweight = 'bold',
           color = 'red', 
           horizontalalignment = 'center')
  
# To save the above image, execute the 
# line- pylab.savefig('heart.png')

输出:

输出#3