darkor

matplotlib

Apr 5th, 2023
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.54 KB | None | 0 0
  1.     def _build_chart(self, data: List[Dict[str, Any]]) -> str:
  2.         plt.rcParams["figure.figsize"] = [15, 5]
  3.         plt.rcParams["figure.autolayout"] = True
  4.         plt.rcParams['font.size'] = 18
  5.  
  6.         x_data, y_data, colors = [], [], []
  7.  
  8.         for record in data:
  9.             index = int(record.get("index"))
  10.  
  11.             if self.current_date.day < index:
  12.                 value, color = record.get("expected"), "#00B690"
  13.  
  14.             else:
  15.                 value, color = record.get("current"), "#22005B"
  16.  
  17.             plt.bar(index, value, color=color)
  18.             x_data.append(index)
  19.  
  20.         plt.xticks(x_data)
  21.  
  22.         plt.gca().axes.get_yaxis().set_visible(False)
  23.  
  24.         plt.gca().axes.spines['top'].set_visible(False)
  25.         plt.gca().axes.spines['left'].set_visible(False)
  26.         plt.gca().axes.spines['right'].set_visible(False)
  27.         plt.gca().axes.spines['bottom'].set_visible(False)
  28.         plt.margins(x=0)
  29.  
  30.         plt.tick_params(bottom=False)
  31.  
  32.         s = io.BytesIO()
  33.         plt.savefig(s, bbox_inches='tight', pad_inches=0, format='png')
  34.         plt.close()
  35.  
  36.         uploaded_image = upload_file_by_credentials_from_config(
  37.             base64_image=base64.b64encode(s.getvalue()).decode("utf-8").replace("\n", ""),
  38.             upload_to=config.COMPANIES_CHARTS_UPLOAD_TO,
  39.             is_private=False,
  40.             is_image=True,
  41.         )
  42.  
  43.         return AwsS3Processor.generate_url_to_file(
  44.             file_name=uploaded_image, bucket_name=config.AWS_IMAGES_S3_BUCKET, is_private=False
  45.         )
Add Comment
Please, Sign In to add comment